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