java AnnotationException 引用的属性不是 (One|Many)ToOne
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28710346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AnnotationException Referenced property not a (One|Many)ToOne
提问by Amit Agarwal
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.Department.departmentId in mappedBy of com.student.information.service.DepartmentHead.department
Both the entities are almost identical. Department can exists with out department head.
两个实体几乎相同。部门可以在没有部门负责人的情况下存在。
Department.Java
部门.Java
@Entity
@Table(name="department", catalog="student")
public class Department {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer departmentId;
@Column(name="dept_name")
private String departmentName;
@OneToMany(mappedBy="department",cascade = CascadeType.ALL)
private List<Student> student;
@OneToOne(targetEntity=Department.class)
private DepartmentHead departmenthead;
}
DepartmentHead.java
部门主管.java
@Entity
@Table(name="departmenthead", catalog = "student")
public class DepartmentHead {
//This is mapped with the department id
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="headname")
private String headName;
@OneToOne(mappedBy = "departmentId",fetch = FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name="dept_id")
private Department department;
}
Caused by: org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne:
引起:org.hibernate.AnnotationException: 引用的属性不是 (One|Many)ToOne:
can someone please point me in the right direction about what mistake am I making. I am struggling from past 2 days and not able to figure out the solution for the issue. Thanks in advance for help.
有人可以指出我犯了什么错误的正确方向。我在过去 2 天里一直在挣扎,无法找出问题的解决方案。预先感谢您的帮助。
回答by JamesENL
You have incorrectly set up your mapping. Hibernate is complaining that no field called departmentIdis available to set up a one to one or many relationship, and it is correct.
您错误地设置了映射。Hibernate 抱怨没有调用的字段departmentId可用于建立一对一或多的关系,这是正确的。
You want to map your values like this.
您想像这样映射您的值。
Department.Java
部门.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
部门主管.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 Departmentfield in DepartmentHeadat the DepartmentHeadfield inside the Department. Hibernate sorts out what ID's to use, you don't have to specify that in the actual link.
你点Department在外地DepartmentHead的DepartmentHead内场Department。Hibernate 整理出要使用的 ID,您不必在实际链接中指定它。
回答by Swathi
How about the Student class. Have you defined ManyToOne relationship in the Student class.
学生班怎么样。您是否在 Student 类中定义了 ManyToOne 关系。
@Entity
public class Student {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@ManyToOne
private Department department;
..................
Please check this example.
请检查这个例子。

