gpt4 book ai didi

java - AnnotationException 引用的属性不是 (One|Many)ToOne

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:08:24 24 4
gpt4 key购买 nike

我试图建立一对一的关系。但我得到错误:

AnnotationException Referenced property not a (One|Many)ToOne
on
com.student.information.service.Department.departmentId in mappedBy of com.student.information.service.DepartmentHead.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: 引用的属性不是 (One|Many)ToOne:

有人可以指出我犯了什么错误的正确方向吗?过去 2 天我一直在苦苦挣扎,无法找出问题的解决方案。在此先感谢您的帮助。

最佳答案

您的映射设置不正确。 Hibernate 提示没有名为 departmentId 的字段可用于建立一对一或多对关系,这是正确的。

您想像这样映射您的值(value)观。

部门.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;
}

您将 DepartmentHead 中的 Department 字段指向 Department 内的 DepartmentHead 字段。 Hibernate 会整理出要使用的 ID,您不必在实际链接中指定它。

关于java - AnnotationException 引用的属性不是 (One|Many)ToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28710346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com