-->
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.  [ 2 posts ] 
Author Message
 Post subject: High number of selects when loading a collection
PostPosted: Wed Apr 07, 2010 1:05 pm 
Newbie

Joined: Wed Apr 07, 2010 12:24 pm
Posts: 1
Hello,

I have a strange issue that I'm seeing. Granted it could very well be my understanding but here is what's going on. Say a 3 entity classes A, B, and C. Class A has a collection of class B's. Class B, is essentially a join table between A and C where the primary key of class B is a composite key of class A and class C. See code below for a general idea of what I'm getting at.

Code:
class A
{
  ...

  @OneToMany(fetch = lazy, mappedBy = "a")
  Set<B> bSet = new HashSet<B>();

   hashcode (...)

   equals (...)

  ...
}

class bPK
{
  @ManyToOne(fetch = lazy)
  A a;

  @ManyToOne(fetch = lazy)
  C c;
}

@IdClass(bPK.class)
class B
{
   @Id
   A a;

   @Id
   C c;

   hashcode (...)

   equals (...)
}

class C
{
   @Id
   String id;

   hashcode (...)

   equals (...)
}


The hashcode and equals implementations on Class A and Class C both compare the id property. The hashcode and the equals on class B compare entities A and C for equality, which in turn would call A and C's equal method.

Now, here is what I'm seeing. Assume I have 6 entities of Class B, all associated with Class A but different Class C's. Every time I loop through Class A's set of B's, it executes one select sql statement to retrieve class C. So, in this example after looping through the whole set, 6 select statement would have been executed. Obviously, 6 isn't so bad but as the number of records increases, then too will the number of select statements.

If anyone has insight that would be great. I'm most likely missing a fundamental understanding about these relationships and this may be behaving correctly. If so, it would be great for an explanation or possibly a workaround.

Thanks!
Mauro


Top
 Profile  
 
 Post subject: Re: High number of selects when loading a collection
PostPosted: Thu Apr 08, 2010 8:38 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I would try to define the ManyToOne relation from B to C eager.

Code:
class bPK
{
  @ManyToOne(fetch = lazy)
  A a;

  @ManyToOne(fetch = eager)
  C c;
}


Theoretically then the navigation query which is implicitely made when accessing a.bSet
should load also all associated C's through an outer join.


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