Hibernate - 更新具有多对多关系的对象

时间:2013-04-25 14:58:21

标签: java hibernate many-to-many

我有类Person和类Role。 Person和Role之间存在多对多的双向关系。如果我创建新的Person,从db加载现有角色并将角色设置为Person,它就可以工作。但是如果我从db加载现有Person(已经有一些角色),从db加载另一个角色,将角色设置为person - 我得到org.hibernate.NonUniqueObjectException具有相同标识符值的另一个对象已经与会话关联:角色。

我不知道问题出在哪里。

有我的hbm.xml

<class name="Person" table="person">
  <id column="ID" name="id" type="java.lang.Integer">
    <generator class="native"/>
  </id>
  <property column="Name" name="name" type="string"/>
  <property column="Surname" name="surname" type="string"/>
 <property column="Email" name="email" type="string"/>    
 <set  name="roles" table="role_person" inverse="false" lazy="true" fetch="select" cascade="all">
    <key column="Person_ID"/>
    <many-to-many class="Role" column="Role_ID"/>
 </set>
</class>

<class name="Role" table="role">
  <id column="ID" name="id" type="java.lang.Integer">
    <generator class="native"/>
  </id>
  <property column="Name" name="name" type="string"/> 
  <set name="people" table="role_person" inverse="true" lazy="true" fetch="select">
    <key column="Person_ID"/>
    <many-to-many class="Role" column="Role_ID"/>
  </set>
 </class>

我的代码:

Person person1 = LoadObjects.getPerson( with some ID); // this person has already role author
Person person2 = LoadObjects.getPerson( with another ID); // this person has already role owner 

Role roleOwner = LoadObjects.getOwnerRole();
Role roleAuthor = LoadObjects.getAuthorRole();

//这个条件不能正常工作,它们总是正确的,即使它们不应该,但在这个例子中它们应该是真的

if (!person1.getRoles().contains(roleOwner)) {
        person1.getRoles().add(roleOwner);
}
if (!person2.getRoles().contains(roleAuthor)) {
        person2.getRoles().add(roleAuthor);
}

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();

session.saveOrUpdate(person1);
session.saveOrUpdate(person2);

session.getTransaction().commit();
session.close();

有getOwnerRole方法:

 public static Role getOwnerRole() {
    Session s = sessionFactory.openSession();
    s.beginTransaction();
    List a = s.createQuery("from Role where name = 'Owner'").list();
    Role role = (Role) a.get(0);
    Hibernate.initialize(role.getPeople());
    s.close();
    return (Role) a.get(0);
 }

1 个答案:

答案 0 :(得分:0)

在您的HBM映射中出现一些错误。认为这不是问题的原因。

<class name="Person" table="person">
  <id column="ID" name="id" type="java.lang.Integer">
    <generator class="native"/>
  </id>
  <property column="Name" name="name" type="string"/>
  <property column="Surname" name="surname" type="string"/>
 <property column="Email" name="email" type="string"/>    
 <set  name="roles" table="role_person" inverse="false" lazy="true" fetch="select" cascade="all">
    <key column="Person_ID"/> // This Should be ID As person id column name is ID..
    <many-to-many class="Role" column="Role_ID"/>// This Should be ID As well as ROLE id column name is ID..
 </set>
</class>

<class name="Role" table="role">
  <id column="ID" name="id" type="java.lang.Integer">
    <generator class="native"/>
  </id>
  <property column="Name" name="name" type="string"/> 
  <set name="people" table="role_person" inverse="true" lazy="true" fetch="select">
    <key column="Person_ID"/>// This Should be ID As ROLE id column name is ID..
    <many-to-many class="Role" column="Role_ID"/>// This Should be ID As well, as People  id column name is ID..
  </set>
 </class>