-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Error when Composite Id is foreign key
PostPosted: Thu May 04, 2006 5:33 am 
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


Top
  
 
 Post subject:
PostPosted: Thu May 04, 2006 5:42 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Change your third mapping file. Either you will have this mapping file

Code:
<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>
</class>


or

Code:
<class name="UserGroup" table="USER_GROUP">
  <composite-id name="id" class="UserGroupKey">
    <key-property name="group" column="GROUP_ID"/>
    <key-property name="user" column="USER_ID" />
  </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>


The second approach is preferable for now as per documentation. Please see this link http://www.hibernate.org/117.html#A34


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 6:00 am 
bkmr_77 wrote:
Change your third mapping file. Either you will have this mapping file


Code:
<class name="UserGroup" table="USER_GROUP">
  <composite-id name="id" class="UserGroupKey">
    <key-property name="group" column="GROUP_ID"/>
    <key-property name="user" column="USER_ID" />
  </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>



sorry this did't helped, now i got
Hibernate: update USER_GROUP set USER_ID=null where USER_ID=?
- SQL Error: 1407, SQLState: 72000
- ORA-01407: cannot update ("WLP_USER"."USER_GROUP"."USER_ID") to NULL

- SQL Error: 1407, SQLState: 72000
- ORA-01407: cannot update ("WLP_USER"."USER_GROUP"."USER_ID") to NULL


I get the Set of UserGroup from first User Table and added one more row to it and tried to update.! it is making them null??[/code]


Top
  
 
 Post subject:
PostPosted: Thu May 04, 2006 6:41 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Actually there is an error in the second form of mapping file. I reused the name attributes which should be different. I hope you have the corrected one in your file.

Code:
<class name="UserGroup" table="USER_GROUP">
  <composite-id name="id" class="UserGroupKey">
    <key-property name="group" column="GROUP_ID"/>
    <key-property name="user" column="USER_ID" />
  </composite-id>

  <many-to-one name="userCollection" column="USER_ID" class="User" not-null="true" insert="false" update="false"/>

  <many-to-one name="groupCollection" column="GROUP_ID" class="Group" not-null="true" insert="false" update="false"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 6:56 am 
bkmr_77 wrote:
Actually there is an error in the second form of mapping file. I reused the name attributes which should be different. I hope you have the corrected one in your file.



the variable names is not an issue both were in different files,
UserGroup and UserGroupKey.


Top
  
 
 Post subject:
PostPosted: Thu May 04, 2006 8:24 am 
Newbie

Joined: Thu May 04, 2006 8:13 am
Posts: 3
Even i am facing the same issue.
If someone can come up with solution,that will be of great help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 12:00 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
however I try to understand, these lines are confusing me. Could you explain in words what you are trying to achieve.

Code:
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);


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 12:07 am 
The scenario is like this.
UserId's are in USER table
GroupId's are in GROUP table

i have to add user to some group...

this user is already in some other group...

so, there is already some entry for that user in USER_GROUP table

I made a query on GROUP table and get a Set of entries in USER_GROUP table(one to many),

Code:
Query query2 = session.createQuery("from Group as gs where gs.groupId = ?");
---
Set ugSet = group.getUserGroupHierarchy();


then i added another object in this Set,

Code:
UserGroup groupHierarchy = new UserGroup();
groupHierarchy.setUser(user);
groupHierarchy.setGroup(group);
UserGroupKey key = new UserGroupKey();
key.setGroup(group);
key.setUser(user);
groupHierarchy.setId(key);


to remove the issue of shared references i copied all the Set entries in some other Set,

Code:
Set userGH = ugSet;

userGH.add(groupHierarchy);


and save() the USER table with new Set formed (with new entry).

Since the composite id of USER_GROUP table is also a foreign key,
i gave the reference of same User object.

Code:
groupHierarchy.setUser(user);
groupHierarchy.setGroup(group);


As per your advice i modified the third mapping file, but during update the hibernate tries to set null in existing data in USER_GROUP.


Top
  
 
 Post subject: please help
PostPosted: Wed May 31, 2006 5:38 am 
Newbie

Joined: Thu May 04, 2006 8:13 am
Posts: 3
Can anyone please help on this.
Even i am facing the same issue.


Top
 Profile  
 
 Post subject: Re: please help
PostPosted: Wed May 31, 2006 5:39 am 
Newbie

Joined: Thu May 04, 2006 8:13 am
Posts: 3
Alexf wrote:
Can anyone please help on this.
Even i am facing the same issue.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.