-->
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: Collections putted into PersistenceContext problem
PostPosted: Thu Mar 01, 2012 6:53 am 
Newbie

Joined: Wed Feb 29, 2012 6:15 am
Posts: 3
Hi,
I use Hibernate 3.3.1.GA as JPA implementation and EJB3's EntityManager with extended persistence context.

Let's say I have entity A and entity B. Entity A has inside collection (OneToMany) of B entities.

And then, in one transaction I execute two HQL queries that return same entity A and this query makes inner join fetch to initialize collections of entities B.

This two HQL queries are different in WHERE clause:
in first I have WHERE B.property1 = 1
in second I have WHERE B.property2 = 1

The results should be the same entity A but the collections of entities B should be different (because of the WHERE clause).

But after the first HQL gets executed, entity A and collection are putted into PersistenceContext and the second HQL query returns same enitity A with same collection of entities B.

Is this correct behaviour? I understand that in the same transaction data should the same, but in this situations HQL queries are different so I expect that Hibernate will recognize this.

Could you please help me with the solutions (I know I can detach the entity but it's quite ugly).

Thanks in advanced
Rob


Top
 Profile  
 
 Post subject: Re: Collections putted into PersistenceContext problem
PostPosted: Thu Mar 01, 2012 7:22 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
How dou you declare the OneToMany relation exactly?
Are you declaring EAGER fetch there?


Top
 Profile  
 
 Post subject: Re: Collections putted into PersistenceContext problem
PostPosted: Thu Mar 01, 2012 7:40 am 
Newbie

Joined: Wed Feb 29, 2012 6:15 am
Posts: 3
pb00067 wrote:
How dou you declare the OneToMany relation exactly?
Are you declaring EAGER fetch there?


No, it's declared as LAZY.


Top
 Profile  
 
 Post subject: Re: Collections putted into PersistenceContext problem
PostPosted: Thu Mar 01, 2012 8:49 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
usil wrote:
pb00067 wrote:
How dou you declare the OneToMany relation exactly?
Are you declaring EAGER fetch there?


No, it's declared as LAZY.

Then I cannot understand why your query implicitely makes inner join fetch to initialize collections of entities B.

Can you please post your code where you define and exectute both HQL queries.


Top
 Profile  
 
 Post subject: Re: Collections putted into PersistenceContext problem
PostPosted: Thu Mar 01, 2012 9:22 am 
Newbie

Joined: Wed Feb 29, 2012 6:15 am
Posts: 3
pb00067 wrote:
usil wrote:
pb00067 wrote:
How dou you declare the OneToMany relation exactly?
Are you declaring EAGER fetch there?


No, it's declared as LAZY.

Then I cannot understand why your query implicitely makes inner join fetch to initialize collections of entities B.

Can you please post your code where you define and exectute both HQL queries.


Maybe I could explain it a little bit more on the simplified code. I would like not to put exact code here because it is company's code, so I changed the names a little bit.

Both queries as declared as namedQuery and entities are like this (assume that entity A means ServiceGroup and entity B means ServiceType):

Code:
@Entity
@Table(name = "SERVICE_GROUP")
@NamedQueries({   
      @NamedQuery(name = "ServiceGroup.findAllUsedAndStandardServicesById", query = "SELECT distinct d FROM ServiceGroup d inner join fetch d.serviceTypeCollection t where d.id=t.serviceGroupId and t.isStandard = 1 and t.isUsed=1 and d.id = :id"),
      @NamedQuery(name = "ServiceGroup.findAllUsedAndExtendedServicesById", query = "SELECT distinct d FROM ServiceGroup d inner join fetch d.serviceTypeCollection t where d.id=t.serviceGroupId and t.isExtended = 1 and t.isUsed=1 and d.id = :id")})
public class ServiceGroup {
   
   @Id
   @Column(name = "ID")
   private Long id;

   
   @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy = "serviceGroupId")
   private Set<ServiceType> serviceTypeCollection;
}

@Entity
@Table(name = "SERVICE_TYPE")
public class ServiceType {

   
   @Id
   @Column(name = "ID")
   private Long id;

   
   @JoinColumn(name = "SERVICE_GROUP_ID", referencedColumnName = "ID")
   @ManyToOne(optional = false)
   private ServiceGroup serviceGroupId;

}


Both queries are called from code like this which implements particular use case:

Code:
Query query = em.createNamedQuery(namedQueryName);
query.setParameter(parameterName, parameterValue);
query.getResultList();



where em is of course EntityManager.
The parameterValue is the same for both queries (the same id of ServiceGroup entity). The difference in these both queries is condition: t.isStandard = 1 in first query and t.isExtended = 1 in second one.

The LAZY fetch type is set because in some other use cases there is no need to retrieve serviceTypeCollection.

I hope this helps. If I cut the code too much and more details are needed, let me know. Thanks


Top
 Profile  
 
 Post subject: Re: Collections putted into PersistenceContext problem
PostPosted: Thu Mar 01, 2012 10:43 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi usil,

The FETCH keyword in the FROM clause tells Hibernate that the relation collection should be eagerly fetched.
When executed, it returns a list of instances, with their collections full initialized.
The second query does not affect (reload) the collection anymore, because it is already marked initialized.

Solution:
Actually you are apparently more interested on the serviceTypes than the serviceGroup,
so I suggest to use following queries:

Code:
"FROM ServiceType t where t.isStandard = 1 and t.isUsed=1 and t.serviceGroupId = :id"
"FROM ServiceType t where t.isExtended = 1 and t.isUsed=1 and t.serviceGroupId = :id"


This will return you 2 distinct and correct result-sets of ServiceType's all belonging to the same serviceGroupId with id = :id


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.