AnnotationException引用的属性不是(One | Many)ToOne [英] AnnotationException Referenced property not a (One|Many)ToOne
问题描述
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ AnnotationException引用的属性不是一个很多$
com 。com.student.information.service.DepartmentHead.department
的mappedBy中的.student.information.service.Department.departmentId两个实体几乎完全相同。部门可以不存在部门负责人。
@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>学生;
@OneToOne(targetEntity = Department.class)
Private DepartmentHead departmenthead;
}
DepartmentHead.java
@Entity
@Table(name =departmenthead,catalog =student)
public class departmentHead {
//这与部门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)
私人部门部门;
$ b引起:org.hibernate.AnnotationException:很多)ToOne:
有人可以请我指出我正在做出什么错误的正确方向。我从过去两天开始挣扎,无法找出问题的解决方案。感谢您的帮助。
解决方案您错误地设置了您的映射。 Hibernate抱怨没有一个名为 departmentId
的字段可用于建立一对一或多个关系,并且这是正确的。
你想像这样映射你的值。
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)
私人部门部门;
}
您指向部门
DepartmentHead
位于部门内的 DepartmentHead
字段中的< 。 Hibernate分类出使用的ID,你不必在实际的链接中指定。
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
@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
@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:
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.
解决方案 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.
这篇关于AnnotationException引用的属性不是(One | Many)ToOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!