java - Hibernate - updating object with many to many relationship -
i have class person , class role. there many many bidirectional relationship between person , role. if create new person, load existing role db , set role person, works. if load existing person db (already has roles), load role db, set role person - org.hibernate.nonuniqueobjectexception different object same identifier value associated session: role.
i don't know problem.
there 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>
my code:
person person1 = loadobjects.getperson( id); // person has role author person person2 = loadobjects.getperson( id); // person has role owner role roleowner = loadobjects.getownerrole(); role roleauthor = loadobjects.getauthorrole();
// conditions don't work properly, true, if shouldn't, in example should true
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();
there getownerrole method:
public static role getownerrole() { session s = sessionfactory.opensession(); s.begintransaction(); list = s.createquery("from role name = 'owner'").list(); role role = (role) a.get(0); hibernate.initialize(role.getpeople()); s.close(); return (role) a.get(0); }
in hbm mapping errors. thought definatel thats not cause of issue.
<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"/> // should id person id column name id.. <many-to-many class="role" column="role_id"/>// should id role id column name 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"/>// should id role id column name id.. <many-to-many class="role" column="role_id"/>// should id well, people id column name id.. </set> </class>