Hibernate 中多个 JQPL 获取连接失败
使用 Hibernate 3.6.7 和 JPA 2,我不能在一个查询中有两个 fetch join。实体有一个称为父级的自引用字段。 localizedTexts 是 Java 类型的 Map 的 @ElementCollection
。 entity.getParent() 有一个带有 EAGER 加载策略的 @ManyToOne。
这是实体的样子:
@Entity
public class Entity extends BaseEntity {
/* ... */
@ManyToOne(fetch = FetchType.EAGER)
public Entity getParent() {
return parent;
}
@ElementCollection
@MapKeyColumn(name = "language")
@Column(name = "text")
public Map<String, String> getLocalizedTexts() {
return localizedTexts;
}
/* ... */
}
以下两个查询有效:
select e from Entity e join fetch e.parent.localizedTexts
select e from Entity e join fetch e.localizedTexts
但这不起作用:
select e from Entity e join fetch e.localizedTexts join fetch e.parent.localizedTexts
Hibernate 抱怨: 查询指定联接获取,但所获取关联的所有者不存在于选择列表中 [FromElement{explicit,collection join,fetch join,fetch non-lazy properties,classAlias=null,role=net.company.project .Entity.localizedTexts,tableName={none},tableAlias=localizedt3_,origin=null,columns={,className=null}}] [从net.company.project.Entity e join fetch e.localizedTexts join fetch e.parent.localizedTexts]
Using Hibernate 3.6.7 and JPA 2, I cannot have two fetch joins in one query. Entity has a self referencing field called parent. localizedTexts is an @ElementCollection
, of Java type of Map. entity.getParent() has a @ManyToOne with EAGER loading strategy.
Here is How entity looks like:
@Entity
public class Entity extends BaseEntity {
/* ... */
@ManyToOne(fetch = FetchType.EAGER)
public Entity getParent() {
return parent;
}
@ElementCollection
@MapKeyColumn(name = "language")
@Column(name = "text")
public Map<String, String> getLocalizedTexts() {
return localizedTexts;
}
/* ... */
}
The following two queries work:
select e from Entity e join fetch e.parent.localizedTexts
select e from Entity e join fetch e.localizedTexts
But this doesn't work:
select e from Entity e join fetch e.localizedTexts join fetch e.parent.localizedTexts
Hibernate complains:query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,collection join,fetch join,fetch non-lazy properties,classAlias=null,role=net.company.project.Entity.localizedTexts,tableName={none},tableAlias=localizedt3_,origin=null,columns={,className=null}}] [select e from net.company.project.Entity e join fetch e.localizedTexts join fetch e.parent.localizedTexts]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想预加载实体“父级”关联以及父级的“localizedTexts”关联,您应该声明以正确顺序遍历模型树的连接:
If you want to preload the Entities "parent" associations as well as the parent's "localizedTexts" association you should declare joins that traverse the model tree in the correct order: