JPA 2.1实体图返回重复的结果 [英] JPA 2.1 Entity Graph returns duplicated results
问题描述
我开始使用JPA 2.1中的新实体图形功能来指定必须加载的Lazy集合。
考虑以下类:
@Entity
@NamedQueries({
@NamedQuery(name =findWithFilterAttr,从a a中选择一个,其中a.filterAttribute类似于:filter)
})
@NamedEntityGraphs({
@NamedEntityGraph(name =graph.childs,attributeNodes = @NamedAttributeNode(childs))})
public class A {
@Id
private long id;
@OneToMany(mappedBy =parent)
私人列表< B>孩子的;
private String filterAttribute;
}
@实体
公共类B {
@Id
私人长ID;
@ManyToOne
私人父母;
}
当我执行指定的查询以获取具有实体的A实体列表图形提示我得到一个具有重复A实体的集合。
我如何才能加载A实体一次。
我正在使用:
我终于解决了这个问题,我将DISTINCT添加到了命名查询中,现在一切正常......
错误是由于当JPA Provider找到一个实体图提示时,它创建了一个LEFT JOIN与子表。
原始查询无实体图:
11:55:28,950 INFO [stdout](默认任务-23)Hibernate :
11:55:28,950 INFO [stdout](默认任务-23)select
11:55:28,951 INFO [stdout](默认任务-23)entitya0_.id as id1_0_0_,
11:55:28,951 INFO [stdout](默认任务-23)作为filter2_0_0_,
的entitya0_.filter INFO [stdout](默认任务-23)childs1_.id作为id1_1_1_,
11:55:28,951 INFO [ 11:55:28,951 INFO [stdout](默认任务-23)childs1_.parent_id作为parent_i2_0_0__,
作为parent_i2_1_1_,
11:55:28,951 INFO [stdout] 11:55:28,951 INFO [stdout](默认任务23)childs1_.id as id1_1_0__
11:55:28,951信息[标准输出](默认任务23)from
11:55:28,951信息INFO [stdout](默认任务-23)(默认任务-23)EntityA entitya0_
11:55:28,951 INFO [stdout](默认任务-23)left outer join
11:55:28,952 INFO [stdout] 23)E ntityB childs1_
11:55:28,952 INFO [stdout](默认任务23)entitya0_.id = childs1_.parent_id
11:55:28,952 INFO [stdout](默认任务-23)其中
11:55:28,952 INFO [stdout](默认任务-23)entitya0_.filter像?
具有不同实体图的查询:
<$ (标准任务-24)休眠:
11:57:25,052 INFO [标准输出](默认任务-24)选择
11:57:25,052 INFO [stdout](默认任务24)childs1_.id作为id1_1_1_,$ b 11:57:25,052 INFO [stdout](默认任务24)不同的entitya0_.id作为id1_0_0_,
11:57:25,052 $ b 11:57:25,052 INFO [stdout](默认任务-24)作为parent_i2_1_1_的$ childs1_.parent_id,$ b 11:57:25,052 INFO [stdout](默认任务-24)entitya0_.filter作为filter2_0_0_, $ b 11:57:25,052 INFO [stdout](默认任务-24)childs1_.id作为id1_1_0__ $ b $ childs1_.parent_id作为parent_i2_0_0__,
11:57:25,052 INFO [stdout] b 11:57:25,052 INFO [stdout](默认任务24)EntityA entitya0_
11:57:25,052 INFO [stdout] ](默认任务-24)left outer join
11:57:25,052 INF O [stdout](默认任务24)EntityB childs1_
11:57:25,052 INFO [stdout](默认任务24)关于entitya0_.id = childs1_.parent_id
11:57:25,052 INFO [ stdout](默认任务-24)其中
11:57:25,052 INFO [stdout](默认任务-24)entitya0_.filter像?
I started using the new entity graph feature in JPA 2.1 to specify the Lazy collections that must be loaded. Consider following classes:
@Entity
@NamedQueries({
@NamedQuery(name="findWithFilterAttr","select a from A a where a.filterAttribute like :filter")
})
@NamedEntityGraphs({
@NamedEntityGraph(name = "graph.childs", attributeNodes = @NamedAttributeNode("childs"))})
public class A{
@Id
private long id;
@OneToMany(mappedBy="parent")
private List<B> childs;
private String filterAttribute;
}
@Entity
public class B{
@Id
private long id;
@ManyToOne
private A parent;
}
When I execute the named query to obtain a list of A entities with the entity graph hint I get a collection with repeated A entities. How can I load the A entities only once.
I'm using:
- Hibernate 4.3.5
- Wildfly 8.1
I finally solved this, I added DISTINCT to the named query and everything works now.... The error was caused because when the JPA Provider find a entity graph hint, it creates a LEFT JOIN with the child table. Original Query without entity graph:
11:55:28,950 INFO [stdout] (default task-23) Hibernate:
11:55:28,950 INFO [stdout] (default task-23) select
11:55:28,951 INFO [stdout] (default task-23) entitya0_.id as id1_0_0_,
11:55:28,951 INFO [stdout] (default task-23) childs1_.id as id1_1_1_,
11:55:28,951 INFO [stdout] (default task-23) entitya0_.filter as filter2_0_0_,
11:55:28,951 INFO [stdout] (default task-23) childs1_.parent_id as parent_i2_1_1_,
11:55:28,951 INFO [stdout] (default task-23) childs1_.parent_id as parent_i2_0_0__,
11:55:28,951 INFO [stdout] (default task-23) childs1_.id as id1_1_0__
11:55:28,951 INFO [stdout] (default task-23) from
11:55:28,951 INFO [stdout] (default task-23) EntityA entitya0_
11:55:28,951 INFO [stdout] (default task-23) left outer join
11:55:28,952 INFO [stdout] (default task-23) EntityB childs1_
11:55:28,952 INFO [stdout] (default task-23) on entitya0_.id=childs1_.parent_id
11:55:28,952 INFO [stdout] (default task-23) where
11:55:28,952 INFO [stdout] (default task-23) entitya0_.filter like ?
Query with distinct and entity graph:
11:57:25,051 INFO [stdout] (default task-24) Hibernate:
11:57:25,052 INFO [stdout] (default task-24) select
11:57:25,052 INFO [stdout] (default task-24) distinct entitya0_.id as id1_0_0_,
11:57:25,052 INFO [stdout] (default task-24) childs1_.id as id1_1_1_,
11:57:25,052 INFO [stdout] (default task-24) entitya0_.filter as filter2_0_0_,
11:57:25,052 INFO [stdout] (default task-24) childs1_.parent_id as parent_i2_1_1_,
11:57:25,052 INFO [stdout] (default task-24) childs1_.parent_id as parent_i2_0_0__,
11:57:25,052 INFO [stdout] (default task-24) childs1_.id as id1_1_0__
11:57:25,052 INFO [stdout] (default task-24) from
11:57:25,052 INFO [stdout] (default task-24) EntityA entitya0_
11:57:25,052 INFO [stdout] (default task-24) left outer join
11:57:25,052 INFO [stdout] (default task-24) EntityB childs1_
11:57:25,052 INFO [stdout] (default task-24) on entitya0_.id=childs1_.parent_id
11:57:25,052 INFO [stdout] (default task-24) where
11:57:25,052 INFO [stdout] (default task-24) entitya0_.filter like ?
这篇关于JPA 2.1实体图返回重复的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!