AnnotationException Referenced property not a (One|Many)ToOne

后端 未结 2 390
广开言路
广开言路 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:18

    How about the Student class. Have you defined ManyToOne relationship in the Student class.

        @Entity
        public class Student {
          @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
          private int id;
          private String name;
    
          @ManyToOne
         private Department department;
         ..................
    

    Please check this example.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题