spring-boot - 具有多个实体映射冲突的多个 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
类,代码如下:Designation
Employee
@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
}
谁能帮我解决这个问题?在我拥有一个实体之前,它运行良好;一旦我添加了另一个,它就无法启动,并出现错误。请帮我解决问题。
解决方案
在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
}
这将导致像这样的表结构。
员工
- EMP_ID
- 姓名
- 电子邮件
指定
- ID
- 角色
- 薪水
- 员工ID
在OneToMany 和ManyToOne 关联中,我们必须注意,哪个类持有Many端。那一方被称为拥有方。因为该类将包含外键。在这里,我们的指定类包含了很多方面。因此,它将自动创建一个外键。
如果我们在 Employee 类中给出 @JoinColumn。我们仍然会将 Designation 类作为 Owner 类。
您可以参考这篇文章,了解更多详情。
而不是这个,让我告诉你一些你的主要错误。
- 不要给变量名与你的类名相同。
- 通过在 java 文件中将变量名称写为 empId,它将在数据库中生成 EMP_ID。
- 如果列名或表名与您的变量名相同,则可以避免重新提及它。
意味着,如果我的实体类名称是 Employee。我希望我的数据库表名也应该是 EMPLOYEE。我可以避免指定@Table(name="EMPLOYEE")。
我希望它会有所帮助!
推荐阅读
- javascript - 如何让我的 X 按钮删除数组中的特定对象?
- javascript - 反应位置返回未定义
- sql-server - Mac 上 docker 容器中的 MSSql:无法连接到数据库
- powershell - PowerShell Invoke-webrequest 中的等效 curl -F
- c++ - 如何将具有 `next()` 方法的类似迭代器转换为常规的 `begin`/`end` 迭代器对?
- angular - 从base64解码得到文件名
- javascript - Passing Puppeteer log result into contenteditable field (non-input)?
- javascript - 从数据层变量对象捕获数据
- python - SonarScanner 错误“文件中的行超出范围”
- excel - 为什么 VBA 仅在退出 Excel 时在 ComboBox 上生成“未找到方法或数据成员”错误?