首页 > 解决方案 > 具有多个实体映射冲突的多个 JPA 存储库

问题描述

我正在使用 JPA 和 Spring Data Rest 创建一个简单的一对多关系。但我收到了这个错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'designationRepository' defined in com.example.relational.BootRelations.repository.DesignationRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.example.relational.BootRelations.models.Designation

那是因为我有类似and 的@Entity类,代码如下:DesignationEmployee

@Entity
public class Designation {

private String Designation;
private float Salary;

@OneToMany(targetEntity=Employee.class, mappedBy="designation", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name="Emp_Id")
private Set<Employee> employee;

// Getter & Setter and Constructors 
}

现在Employee上课:

@Entity
@Table(name="Employee")
public class Employee {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="Emp_Id")
    private int Emp_Id;
    @Column(name="Emp_Name")
    private String Emp_Name;
    @Column(name="Emp_Email")
    private String Emp_Email;
    
    @OneToOne(targetEntity=Designation.class, mappedBy="employee", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    private Designation designation;

    // // Getter & Setter and Constructors 
}

存储库是:

public interface DesignationRepository extends JpaRepository<Designation, Integer> {}

public interface EmployeeRepository extends JpaRepository<Employee, Integer> {}

服务:

@Service
public class DesignationServiceImpl implements DesignationService {

    private DesignationRepository designationRepository;

    @Autowired
    public DesignationServiceImpl(DesignationRepository designationRepository) {
        this.designationRepository = designationRepository;
    }

    @Override
    public List<Designation> findAllDesignation() {
        return designationRepository.findAll();
    }   
}
public class EmployeeServiceImpl implements EmployeeService {
    
    private EmployeeRepository employeeRepository;
    
    @Autowired
    public EmployeeServiceImpl(@Qualifier("employeeJPAImpl") EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;
    }   
}

    @Service
public class DesignationServiceImpl implements DesignationService {

    private DesignationRepository designationRepository;

    @Autowired
    public DesignationServiceImpl(@Qualifier("DesignationRepository")  DesignationRepository designationRepository) {
        this.designationRepository = designationRepository;
    }

    @Override
    public List<Designation> findAllDesignation() {
        return designationRepository.findAll();
    }
}

因为我有两个实体,所以我有两个控制器:

@RestController
@RequestMapping("/api")
public class DesignationController {

    private DesignationService designationService;

    @Autowired
    public DesignationController(DesignationService designationService) {
        this.designationService = designationService;
    }
    
    @GetMapping("/designation")
    public List<Designation> getAllDesignation(){
        return designationService.findAllDesignation();
    }
    
}


@RestController
@RequestMapping("/api")
public class EmployeeController {

    private EmployeeService employeeService;

    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }
    // Mappings
}

谁能帮我解决这个问题?在我拥有一个实体之前,它运行良好;一旦我添加了另一个,它就无法启动,并出现错误。请帮我解决问题。

标签: spring-boothibernatejpaspring-data-jpaspring-data

解决方案


Employee类中进行此更改。

@Entity
@Table
public class Employee {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private Long empId;
    @Column
    private String Name;
    @Column
    private String Email;
    
    @OneToMany(targetEntity=Designation.class, fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    private Designation designation;

    // // Getter & Setter and Constructors 
}

Designation类中,进行此更改..

@Entity
public class Designation {

@Id
private Long id; 
private String role;
private float salary;

@ManyToOne(targetEntity=Employee.class, fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn
private Set<Employee> employee;

// Getter & Setter and Constructors 
}

这将导致像这样的表结构。

员工

  1. EMP_ID
  2. 姓名
  3. 电子邮件

指定

  1. ID
  2. 角色
  3. 薪水
  4. 员工ID

在OneToMany 和ManyToOne 关联中,我们必须注意,哪个类持有Many端。那一方被称为拥有方。因为该类将包含外键。在这里,我们的指定类包含了很多方面。因此,它将自动创建一个外键。

如果我们在 Employee 类中给出 @JoinColumn。我们仍然会将 Designation 类作为 Owner 类。

您可以参考这篇文章,了解更多详情。

而不是这个,让我告诉你一些你的主要错误。

  1. 不要给变量名与你的类名相同。
  2. 通过在 java 文件中将变量名称写为 empId,它将在数据库中生成 EMP_ID。
  3. 如果列名或表名与您的变量名相同,则可以避免重新提及它。

意味着,如果我的实体类名称是 Employee。我希望我的数据库表名也应该是 EMPLOYEE。我可以避免指定@Table(name="EMPLOYEE")。

我希望它会有所帮助!


推荐阅读