-->
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.  [ 8 posts ] 
Author Message
 Post subject: Clean detachment of object with ManyToMany relation
PostPosted: Mon Aug 13, 2007 6:01 pm 
Newbie

Joined: Thu Jul 19, 2007 2:02 pm
Posts: 5
Hi

My goal is to cleanly detach an object when it has MayToMany to another object and it is not requested by user.

I have a ManyToMany relation between two objects

Role
{
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(...)
public Collection<User> getUsers()
{
return users;
}
}

Class User
{
// manytomay to Roles
}

1. i restart my server to make sure that there are no pending flushes.
2. my goal is to get a list of Roles (without the list of users per role)

When i just want to get a list of roles , i want cleanly detach Role POJO so that getUsers() should give me a new ArrayList or null.

however, when i initialize the users to new ArrayList(), i notice that hibernate

is processing ACTION_PERSIST_ON_FLUSH on the users and there fore, then executes

delete from Role_User where role.id =?

and deletes all my users associated with role. from Role_User table

I do not see this happen to ManyToOne relation.

Questions:

1. what do i set the collection of users to so that i do not see data being deleted from database?
2. if there is any other solution to achieve the same.

Help is very much appreciated as i have broken my head on this for a long time.

thanks
Sudhir

_________________
Thanks
Sudhir


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 6:18 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
If you set the users collection of role to a new (empty) array list while the session is open then hibernate will interpret that as deleting all the users from that role.

Your requirements sound a little odd. Calling getUsers should return the users if there are some. Returning an empty list instead just strikes me as wrong. Either you shouldn't be calling that method in your scenario or it should be pre-initialised with the users form the role.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 6:33 pm 
Newbie

Joined: Thu Jul 19, 2007 2:02 pm
Posts: 5
thanks for the reply

thatmikewilliams wrote:
If you set the users collection of role to a new (empty) array list while the session is open then hibernate will interpret that as deleting all the users from that role.

When i have not asked the object to be persisted or merged, why should hibernate assume that i am cleaning up the relation?


Your requirements sound a little odd. Calling getUsers should return the users if there are some. Returning an empty list instead just strikes me as wrong. Either you shouldn't be calling that method in your scenario or it should be pre-initialised with the users form the role.


I have two modes in which an object is populated
- notDeep ==> where only its attributes and relations marked eager are populated
- deep ==> where its attributes and all relations are populated.

My requirement is to serialize POJOs to XML outside the session in the client code. So, i iterate thru all attributes and relations to build the XML. If i find a relation populated, i serialize the relation to XML as well.
Now, i get this lazyInitError if i access a PersistentBag outside session, which is valid. I am looking for a solution to avoid that exception and have the POJO not have any Persistence pointers/references

in non-deep mode
<role name="rr">
</role>

in deep mode
<role name="rr">
<user name="ss"/>
<user name="dd"/>
</role>

i did download a HibernateCleaner piece of code, but when i run that it removes all peristence references but it always populates ManyToMany relations.

i hope my requirement was clear enough. Your thoughts will be appreciated.

_________________
Thanks
Sudhir


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 6:50 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
So you make a request to get an object in either "deep" or "shallow" mode therefor you already know what kind of data you're going to get back. The XML generator should take account of the data request you made. e.g.
Code:
Entity shallowEntity = dao.getShalowEntity(id);
sendShallowEntityXml(shallowEntity);

Entity deepEntity = dao.getDeepEntity(id);
sendDeepEntityXml(deepEntity);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 6:59 pm 
Newbie

Joined: Thu Jul 19, 2007 2:02 pm
Posts: 5
yes i could do that. however that does not cover the scenario when the same pojo is being passed around between session beans for processing. for example, if i there is a bean to add on other information to the role object being passed, will hibernate not throw an exception if i want to add another user to the user collection?

_________________
Thanks
Sudhir


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 7:02 pm 
Newbie

Joined: Thu Jul 19, 2007 2:02 pm
Posts: 5
is there a way to know if the PersistentBag has been executed to populate itself.

i know executing size() on the bag executes the query, if it is already not done. is there a flag that indicates whether the query is done or not?

_________________
Thanks
Sudhir


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 7:23 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
If you're passing the role between session beans in the same transaction then the hibernate session that loaded the object initially should still be open (unless you're explicitly closing it) so fetching users should be fine.

I'm not sure there's a way of checking whether collections are initialised before accessing them but needing to do so just sounds wrong to me.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 7:29 pm 
Newbie

Joined: Thu Jul 19, 2007 2:02 pm
Posts: 5
whats wrong is when hibernate persists/merges onetoMany or manyToOne only when persist/merge methods are called, why does it handle differently for ManyToMany.

if you are given a POJO and there are public methods there should be no restrictions of not calling it. If EJB3 says that detatching the object from entutymanager creates the value object to be passed around, it should be cleaned up correctly.

anyways, workarounds need to be written i guess.

thanks for your time and help.

_________________
Thanks
Sudhir


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.