Hibernate 多对多与急切加载问题
我对类的定义是这样的:
@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;
}
当
itemSession.createQuery("from Item i where i.url=?").setString(0, url)
需要使用两个 SQL 查询进行获取时:
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=?
如何将其变成一个?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HQL 忽略映射中指定的获取策略。您应该使用强制加入
HQL ignores the fetching strategy specified in mappings. You should force the join using