-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL Query.list() is returning proxy
PostPosted: Wed Dec 01, 2010 2:09 am 
Newbie

Joined: Tue Jul 27, 2010 5:19 am
Posts: 4
select cell from AcqDmProdField dmprod
inner join dmprod.acqCmpCell cell
inner join cell.segment seg
inner join seg.version ver
inner join ver.campaign cmp
where (cmp.campaignNumber= :campaignNumber and dmprod.productionContact = :prodContact) and ver.status = :statusActive

And the relationships are as Follows:

AcqDmProdField < -> AcqCmpCell (One-to-One)
AcqCmpCell <-> AcqCmpSegment (Many-to-One)
AcqCmpSegmet <-> AcqVersion (One-to-One)
AcqVersion <-> AcqCampaign (May-to-One)

Now when I execute query.list() I get two objects of AcqCmpCell in list but only one of them is proxy and the other one as populated.

I want to perform intersection operation between this list and a set(Set interface's retainAll() method). But as one of the objects is
proxy,its not being retained as my equals() method AcqCmpCell checks if the two objects' class is same or not before checking for the buisness keys of those two objects for equality.

This is my implementation of equlas() (though not relavent in this context):

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())// here this condition returns false when object being compared is proxy
return false;
AcqCmpCell other = (AcqCmpCell) obj;
if (cellIndex == null) {
if (other.cellIndex != null)
return false;
} else if (!cellIndex.equals(other.cellIndex))
return false;
return true;
}

Does this something related to session cache?? and the query.list() object returns this from session cache instead of hitting the database??

This is urgent as we need to know how to make query.list() return every object pre-populated and not the proxy??


Top
 Profile  
 
 Post subject: Re: HQL Query.list() is returning proxy
PostPosted: Wed Dec 01, 2010 3:35 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You need to disable proxies by setting lazy=false for your entity. Eg. <class ... lazy="false"> or @Proxy(lazy=false)


Top
 Profile  
 
 Post subject: Re: HQL Query.list() is returning proxy
PostPosted: Wed Dec 01, 2010 5:06 am 
Newbie

Joined: Tue Jul 27, 2010 5:19 am
Posts: 4
No I cannot do that as it is being used throughout my application very frequently. If I disable lazy fetching It would ad additional burden on my app.

What is happening in my case is : If an entity has already been loaded and if same appears in query.list() resultset, hibernate will return proxy in session cache instead of hitting the db and bringing the populated persistent object.

any thoughts??


Top
 Profile  
 
 Post subject: Re: HQL Query.list() is returning proxy
PostPosted: Wed Dec 01, 2010 6:02 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Then you need to change your equals() implementation so that it it also works for proxied and non-proxied objects. Eg. use instanceof for comparing the classes.


Top
 Profile  
 
 Post subject: Re: HQL Query.list() is returning proxy
PostPosted: Wed Dec 01, 2010 9:11 am 
Newbie

Joined: Tue Jul 27, 2010 5:19 am
Posts: 4
hmmm..I have done that ..but thought its a patch and not a convincing solution.
Are you sure there is no way to fetch populated objects with query.list() when there is entity loaded in session cache.??


Top
 Profile  
 
 Post subject: Re: HQL Query.list() is returning proxy
PostPosted: Wed Dec 01, 2010 9:31 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Even if you can fix this use case to always return non-proxied objects (eg. by session.clear() or something), it is not a very future-proof fix. Whenever you start adding objects to a Set, Map or doing something else that depends on the equals() implementation, you will have to make sure that you are not using proxies or it will break.


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