相互关联的JPA注解



我正在设计一个带有spring引导,jpa和h2的微服务应用程序。我有三个实体雇员,部门和组织在我的服务之一。我已经为组织和部门表中的列表和列表标注了关系,而不是私人部门部门;和私人组织机构;在Employee表中,如下所示。但我在例外以下。你能告诉我怎么解决吗?

@Data
@Entity
class Employee{
public Employee(String name,int age,String email,String designation) {
this.name=name;
this.age=age;
this.email=email;
this.designation=designation;


}
@Id
private long id;
private String name;
private int age;
private String email;
private String designation;
@ManyToOne
@JoinColumn(name = "departmentId")
private Department department;
@ManyToOne
@JoinColumn(name = "organizationId")
private Organization organization;
}
@Data
@Entity
class Department{
public Department(long organizationId,String name) {
this.organizationId=organizationId;
this.name=name;
}

@Id
private long id;
private Long organizationId;
private String name;
@OneToMany(mappedBy = "department")
private List<Employee> employees = new ArrayList<>();
}
@Data
@Entity
class Organization{
public Organization(String name,String address) {
this.name=name;
this.address=address;
}

@Id 
@GeneratedValue
private long id;

private String name;
private String address;
@OneToMany(mappedBy = "department")
private List<Employee> employees = new ArrayList<>();
@OneToMany(mappedBy = "organization")
private List<Department> departments = new ArrayList<>();
}

异常

Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.example.demo.Department.organization in com.example.demo.Organization.departments
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:848) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:799) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:53) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1693) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1661) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:295) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1858) ~[spring-beans-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1795) ~[spring-beans-5.2.13.RELEASE.jar:5.2.13.RELEASE]
... 22 common frames omitted

@Entity
@Table(name = "DEPARTMENTS")
class Department {

@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ORGANIZATION_ID")  
private Organization organization;
}
@Entity
@Table(name = "ORGANIZATIONS")
class Organization{
@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
@OneToMany(mappedBy = "organization", fetch = FetchType.LAZY)
private List<Department> departments = new ArrayList<>();
}
  1. Long代替id
  2. long
  3. 始终使用FetchType.LAZY
  4. 使用默认构造函数(如果没有生成)
  5. 如果使用列、表名,则不需要连接列名。

什么是@JoinColumn以及它如何在Hibernate中使用

相关内容

  • 没有找到相关文章

最新更新