Reputation: 721
My definition of class is like:
@Entity
@Table(name = "items")
public class Item {
@Id
@GeneratedValue
private long id;
@Column(unique = true, nullable = false)
private String url;
@ManyToMany(fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
private Collection<Tag> tags;
@Column(columnDefinition = "TEXT")
private String text;
}
and when fetching with
itemSession.createQuery("from Item i where i.url=?").setString(0, url)
two SQL queries are needed:
select item0_.id as id1_,
item0_.text as text1_,
item0_.url as url1_
from items item0_
where item0_.url=?
select tags0_.items_id as items1_1_1_,
tags0_.tags_id as tags2_1_,
tag1_.id as id0_0_, tag1_.lastCrawlTime as lastCraw2_0_0_,
tag1_.name as name0_0_
from items_tags tags0_ inner join tags tag1_ on tags0_.tags_id=tag1_.id
where tags0_.items_id=?
How can I make it into one?
Upvotes: 3
Views: 1719
Reputation: 15240
HQL ignores the fetching strategy specified in mappings. You should force the join using
"from Item i join fetch i.tags where i.url=?"
Upvotes: 2