AnnotationException Referenced property not a (One|Many)ToOne

后端 未结 2 389
广开言路
广开言路 2020-12-14 23:32

I tried to make one-to-one relationship. But I get error:

AnnotationException Referenced property not a (One|Many)ToOne
on
com.student.information.service.D         


        
2条回答
  •  死守一世寂寞
    2020-12-15 00:27

    You have incorrectly set up your mapping. Hibernate is complaining that no field called departmentId is available to set up a one to one or many relationship, and it is correct.

    You want to map your values like this.

    Department.Java

    @Entity
    @Table(name="department", catalog="student")
    public class Department {
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private Integer departmentId;
    
        @OneToOne
        @JoinColumn(name = "id")
        private DepartmentHead departmenthead;
    }       
    

    DepartmentHead.java

    @Entity
    @Table(name="departmenthead", catalog = "student")
    public class DepartmentHead {
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int id;
    
        @OneToOne(mappedBy = "departmenthead")
        private Department department;  
    }
    

    You point the Department field in DepartmentHead at the DepartmentHead field inside the Department. Hibernate sorts out what ID's to use, you don't have to specify that in the actual link.

提交回复
热议问题