Hibernate在entitymanager.flush()上抛出“org.hibernate.HibernateException:在同一个集合中找到两个表示”

时间:2017-02-28 23:33:03

标签: java hibernate jpa collections entity

我目前正在使用Hibernate开发一个中等规模的Java项目,我遇到了一个看似罕见但非常持久的错误。情况如下:我有一个Student实体,它与Education实体(在数据库上实现为连接表)具有双向多对多关系,而Admin实体是Student的子类。我的代码允许学生通过从数据库中删除学生来“升级”到管理员,根据学生创建新的管理员并持久保存此管理员。但是,每当发生这种情况时,Hibernate会在EntityManager.flush()上抛出以下错误:

org.hibernate.HibernateException: Found two representations of same collection: domain.Student.enrolledEducations

您可以在下面找到相关代码:

教育课

@Entity
public class Education {

...

@ManyToMany
@JoinColumn(name = "education_id")
private Set<Course> courses = new HashSet<>();

学生班

@Entity
public class Student {

....

@ManyToMany
@JoinColumn(name = "student_id")
private Set<Education> enrolledEducations = new HashSet<>();

管理员课程

@Entity
public class Admin extends Student {

...

public Admin(Student student) {
    this.setId(student.getId());
    this.setFirstName(student.getFirstName());
    this.setLastName(student.getLastName());
    this.setEmail(student.getEmail());
    this.setSalt(student.getSalt());
    this.setSuperAdmin(false);
    this.setEnrolledEducations(student.getEnrolledEducations());
    this.setSessions(student.getSessions());
    this.setManagedEducations(new HashSet<Education>());
}

数据库方法

public Admin upgrade(Person person) {
    Admin admin;
    if (person instanceof Student){
        removeStudent((Student) person);
        admin = new Admin((Student) person);
    }
    else{
        removePerson(person);
        admin = new Admin(person);

    }
    addAdmin(admin); //exception happens here

    return admin;
}

public void addAdmin(Admin admin) {
    manager.getTransaction().begin();

    if(manager.contains(admin)){
        manager.merge(admin);
    }
    else{
        manager.persist(admin);
    }

    manager.flush(); //exception happens here
    manager.getTransaction().commit();
}

测试方法

@Test
public void getEducationsForAdmin_and_upgrade_to_admin_work_correctly(){

    educationSetup();

    Admin admin1 = facade.upgrade(student1); //exception happens here
    Admin admin2 = facade.upgrade(student2);

    admin1.addNewEducation(education1);
    admin1.addNewEducation(education2);
    admin2.addNewEducation(education1);

    facade.updateAdmin(admin1);
    facade.updateAdmin(admin2);

    Set<Education> educations1 = new HashSet<>(facade.getEducationsForStudent(admin1));
    Set<Education> educations2 = new HashSet<>(facade.getEducationsForStudent(admin2));

    assertTrue("admin 1 does not have exactly 1 education", educations1.size()==1);
    assertTrue("admin 2 does not have exactly 2 educations", educations2.size()==2);
    assertTrue("admin 1 does not have the IT education",educations1.contains(education1));
    assertTrue("admin 2 does not have the IT education",educations2.contains(education1));
    assertTrue("admin 2 does not have the M education",educations2.contains(education2));
}

1 个答案:

答案 0 :(得分:0)

您似乎遇到管理员和学生都有相同标识符的问题。

由于Admin是通过调用new函数创建的,因此它不是持久状态,即代码

manager.contains(admin)

将始终返回false,因此它将始终转到manager.persist语句。

由于Admin是具有相同标识符的不同对象,因此您将获得异常

  

找到两个相同集合的表示

您需要做的就是添加

manager.delete(person)

中的

removePerson

功能。它应该解决这个问题。