[英]Hibernate: OneToMany Association not working Unknown entity error
I am getting the following error when I am trying to create a one-to-many association 尝试创建一对多关联时出现以下错误
Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.hibernate.collection.internal.PersistentBag
Here is where the code to create the association is run 这是运行创建关联的代码的地方
public void assignDoctorSpecialty(Doctor doctor, Specialty specialty) {
Session session = sessionFactory.openSession();
session.beginTransaction();
doctor.setSpecialty(specialty);
List<Doctor> doctors = specialty.getDoctors();
doctors.add(doctor);
session.save(doctors);
session.getTransaction().commit();
session.close();
}
Here is my Doctor class 这是我的医生课
package edu.cs157b.hibernate;
import java.util.ArrayList;
import javax.persistence.*;
@Entity
@Table(name="DOCTOR_INFO")
@NamedQueries (
{
@NamedQuery(name = "Doctor.getAll", query = "from Doctor"),
@NamedQuery(name = "Doctor.findByName", query = "from Doctor where name = :name")
}
)
public class Doctor implements Person {
private int id;
private String name;
private Specialty specialty;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(unique=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne (fetch = FetchType.EAGER, cascade= CascadeType.PERSIST)
@JoinColumn(name="specialty_id")
public Specialty getSpecialty() {
return specialty;
}
public void setSpecialty(Specialty specialty) {
this.specialty = specialty;
}
}
Here is my Specialty class 这是我的专业课
package edu.cs157b.hibernate;
import java.util.ArrayList;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name="SPECIALTY_INFO")
@NamedQueries (
{
@NamedQuery(name = "Specialty.getAll", query = "from Specialty"),
@NamedQuery(name = "Specialty.findByName", query = "from Specialty where name = :name")
}
)
public class Specialty {
private List<Doctor> doctors = new ArrayList<Doctor>();
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(unique=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy="specialty", targetEntity = Doctor.class,
fetch=FetchType.LAZY, cascade= CascadeType.PERSIST)
public List<Doctor> getDoctors() {
return doctors;
}
public void setDoctors(List<Doctor> doctors) {
this.doctors = doctors;
}
}
EDIT 编辑
If I save the Doctor
object directly, instead of trying to indirectly save it through Cascade saving the Specialty
doctor list, it throws no error. 如果我直接保存Doctor
对象,而不是尝试通过Cascade保存Specialty
Doctor列表间接保存它,则不会引发任何错误。 However, when I go into my database, specialty_id
of the Doctor
table is not set. 然而,当我进入我的数据库, specialty_id
了的Doctor
表未设置。
As far as I know it is not possible to persist a collection of objects using the save(..)
method. 据我所知,不可能使用save(..)
方法持久save(..)
对象集合。 Try the following code: 尝试以下代码:
public void assignDoctorSpecialty(Doctor doctor, Specialty specialty) {
[...]
for (Doctor d : doctors) {
session.save(d);
}
session.getTransaction().commit();
session.close();
}
I figured out the answer. 我想出了答案。 The reason this happens is because I try to save
an object that was already saved in the database. 发生这种情况的原因是因为我尝试save
一个已经保存在数据库中的对象。 All I had to do was replace session.save(doctor)
with session.saveOrUpdate(doctor)
. 我要做的就是用session.save(doctor)
替换session.saveOrUpdate(doctor)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.