简体   繁体   English

PersistenceException:传递给持久化的分离实体

[英]PersistenceException: detached entity passed to persist

I read through a lot of threads on stackoverflow about the same exception but I didn't find anything that actually helps me so here I am: 我在stackoverflow上通读了很多关于同一异常的线程,但是我没有发现任何对我有实际帮助的东西,所以我在这里:

I have got 2 entity classes: 我有2个实体类:

Teacher class: 教师班:

@Entity
@Table(name="teachers")
public class Teacher extends Model{

public Teacher()

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

@OneToMany(fetch=FetchType.LAZY, mappedBy="teacher")
private List<Student> students;

//getter and setter
}

Student class: 学生班:

@Entity
@Table(name="students")
public class Student extends Model{

public Student(){}
public Student(Teacher teacher){this.teacher = teacher;}

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
@JoinColumn(name="teacher_id")
private Teacher teacher;

//getter and setter
}

Model class: 型号类别:

public abstract class Model implements Serializable {

public void save() {
EntityManager entityManager = HibernateUtil.getEntityManagerFactory().createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(this);
entityManager.getTransaction().commit();
entityManager.close();
}

public void merge(){
EntityManager entityManager = HibernateUtil.getEntityManagerFactory().createEntityManager();
entityManager.getTransaction().begin();
entityManager.merge(this);
entityManager.getTransaction().commit();
entityManager.close();
}
}

I created a Test class to insert some values into the db: 我创建了一个Test类,将一些值插入db中:

public class TestClass{

Teacher teacher;
teacher = new Teacher();

Student student_1;
Student student_2;

student_1 = new Student(teacher);
student_1.save();

student_2 = new Student(teacher);
student_2.save();

}

student_2.save() throws the exception (teacher is a detached entity). student_2.save()引发异常(老师是一个独立的实体)。 But why? 但为什么? I understand, that teacher is already persisted and the transaction is over but CascadeType.ALL includes merge aswell, which should take care of it (or obviously not..i might be wrong here) but the following then works: 我知道,老师已经存在并且事务结束了,但是CascadeType.ALL也包括merge,它应该处理它(或者显然不是。我在这里可能是错的),但是下面的方法可以工作:

student_2.merge() instead of student_2.save() works though~ 学生_2.merge()代替学生_2.save()可以工作〜

Another solution would be to remove the cascadeType.ALL and persist teacher before persisting student but that's not what I want. 另一个解决方案是在保留学生之前删除cascadeType.ALL并保留教师,但这不是我想要的。

Cascade works different from what you think. 级联的工作方式与您的想法不同。 It calls the same function on the related objects. 它在相关对象上调用相同的函数。 So if you call em.persist(student) it will call persist on teacher. 因此,如果您呼叫em.persist(student),它将在老师身上调用persist。

I think you probably don't want to cascade anything from the student to the teacher. 我认为您可能不想将学生和老师之间的任何级联。 If you persist the teacher in a transaction and then only link it to the students in the other transactions you don't have cascade anything to the teacher. 如果您坚持老师进行某项事务,然后仅将其链接到其他事务中的学生,则您对老师没有任何要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM