0

我有班级人员和班级角色。Person 和 Role 之间存在多对多的双向关系。如果我创建新的人,从数据库加载现有角色并将角色设置为人,它就可以工作。但是,如果我从数据库加载现有人员(已经有一些角色),从数据库加载另一个角色,将角色设置为人员 - 我得到 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);
 }
4

1 回答 1

0

在您的 HBM 映射中存在一些错误。认为definatel那不是问题的原因。

<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>
于 2013-04-25T15:10:59.037 回答