Reputation: 680
I have a common situation with parent-child relation like:
class Parent {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
List<ChildOne> childrenOne;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
List<ChildTwo> childrenTwo;
@OneToMany
List<LazyChild> lazyChildren;
@Id
Long id;
}
Then I have the HQL query like:
select lazyChild from Parent p
join p.lazyChildren lazyChild
where p.id = ? and lazyChild.mnemonic='AAA'
When I execute it I get LazyChild object and it's what I want. But hibernate also initialize all eagerly defined collections and it's what I don't want. It's not intuitive that hibernate makes a separate call to fetch eager associations. I see it by switching to show SQL query.
How to avoid that unnecessary SQL calls?
Upvotes: 0
Views: 1340
Reputation: 3662
I'm not sure I understand what you want to do.
But if you don't want your collections childrenOne
and childrenTwo
to be loaded, you should probably declare them with FetchType.LAZY
.
It seems to me that you're not querying the right entity, if you just want a list of LazyChild
, then your HQL should be something like this :
from LazyChild child
where child.parent.id = ? and child.mnemonic='AAA'
Assuming you have the parent mapped in the LazyChild
entity :
@ManyToOne //(fetch=FetchType.LAZY) if you don't need to have the parent loaded
@JoinColumn(name = "parent_id")
private Parent parent;
Upvotes: 0
Reputation: 6540
By specifying FetchType.EAGER
you've said to Hibernate that every time it loads the parent object, you want it to load up those children objects. If you don't want this to happen, you can't specify an Eager fetch type.
Hibernate isn't smart enough to know that when you are querying the Parent
you only want the lazyChild. All it knows is that you've made a request to the Parent
object, so it needs to load up the eagerly fetch children.
If you are new to Hibernate, you might find my tutorial here helpful.
Upvotes: 1