JPA Annotations to relate each other

I am designing a microservice application with spring boot,jpa and h2.I have 3 entities Employee,Department and Organization in one of my service.I already annotated relationship for the List and List in Organization and Department table against private Department department; and private Organization organization; in Employee table as shown below.But I'm getting below exception.Could you please tell me how to resolve it.

@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<>();

}

Exception

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

1 answer

  • answered 2021-02-24 11:05 v.ladynev

    @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. Use Long in place of long for id
    2. Always use FetchType.LAZY
    3. Use default constructors (if they are not generated)
    4. You don't need a join column name, if you are using column, table names aerogeneration

    what is @JoinColumn and how it is used in Hibernate