Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0.5
Mapping documents:
First
<class name="User" table="USER">
<id name="userId" column="USER_ID" type="java.lang.Long">
<generator class="assigned"/>
</id>
<property name="password" column="PASSWORD" type="java.lang.String" />
<property name="userName" column="USER_NAME" type="java.lang.String" not-null="true" />
<set name="userGroupHierarchy" lazy="false">
<key column="USER_ID"/>
<one-to-many class="UserGroup"/>
</set>
</class>
Second
<class name="Group" table="GROUP">
<id name="groupId" column="GROUP_ID" type="java.lang.Long">
<generator class="assigned"/>
</id>
<property name="groupName" column="GROUP_NAME" type="java.lang.String" not-null="true" />
<set name="userGroupHierarchy" lazy="false">
<key column="GROUP_ID"/>
<one-to-many class="UserGroup"/>
</set>
</class>
Third
<class name="UserGroup" table="USER_GROUP">
<composite-id name="id" class="UserGroupKey">
<key-many-to-one name="group" column="GROUP_ID" class="Group"/>
<key-many-to-one name="user" column="USER_ID" class="User"/>
</composite-id>
<many-to-one name="user" column="USER_ID" class="User" not-null="true" insert="false" update="false"/>
<many-to-one name="group" column="GROUP_ID" class="Group" not-null="true" insert="false" update="false"/>
</class>
Code between sessionFactory.openSession() and session.close():
Query query = session.createQuery("from User as u where u.userId = ?");
query.setParameter(0,userId);
User user = new User();
List list = query.list();
if(list!=null){
user = (User) list.get(0);
}
Query query2 = session.createQuery("from Group as gs where gs.groupId = ?");
query2.setParameter(0,gId);
Group group = new Group();
List list2 = query2.list();
if(list2!=null){
group = (Group) list2.get(0);
}
Set ugSet = group.getUserGroupHierarchy();
UserGroup groupHierarchy = new UserGroup();
// groupHierarchy.setCreationDate(new Date());
// groupHierarchy.setModifiedDate(new Date());
groupHierarchy.setUser(user);
groupHierarchy.setGroup(group);
UserGroupKey key = new UserGroupKey();
key.setGroup(group);
key.setUser(user);
groupHierarchy.setId(key);
Set userGH = ugSet;
userGH.add(groupHierarchy);
userSecurity.setUserGroup(userGH);
session.saveOrUpdate(userSecurity);
Full stack trace of any exception that occurs:
org.hibernate.HibernateException: Found shared references to a collection
at org.hibernate.engine.Collections.processReachableCollection(Collections.java:130)
at org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:37)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:104)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:64)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:58)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:198)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at com.btwholesale.portal.dua.dao.ESRDAOImpl.updateUserGroup(Unknown Source)
Name and version of the database you are using: Oracle 8i
The problem is occuring as the comosite ID is also a foreign key too 2 different tables and data is to be inserted in table with foreign key.. primary key is already there in table...
USER
userId - PK
GROUP
groupId - pk
USER_GROUP
userId
groupId --composite Id
Please Help!
Note:Some non required entries from mapping have been removed as they are not teh part of the problem