Hibernate跨子级对公共表的单个查询?

Hibernate跨子级对公共表的单个查询?,hibernate,Hibernate,我希望hibernate能够对当前正在执行多个查询的内容执行单个查询。我的场景是使用JPA/hibernate注释定义的父对象: @javax.persistence.Entity @Table(name="example") @Inheritance(strategy=InheritanceType.JOINED) public class Example implements Serializable { ... @OneToMany(cascade = CascadeType.ALL, o

我希望hibernate能够对当前正在执行多个查询的内容执行单个查询。我的场景是使用JPA/hibernate注释定义的父对象:

@javax.persistence.Entity
@Table(name="example")
@Inheritance(strategy=InheritanceType.JOINED)
public class Example implements Serializable {
...
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "theId")
@LazyCollection(LazyCollectionOption.FALSE)
@BatchSize(size=1000)
private Set<ChildObjectOne> childrenOne = new LinkedHashSet<>();

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "theId")
@LazyCollection(LazyCollectionOption.FALSE)
@BatchSize(size=1000)
private Set<ChildObjectTwo> childrenTwo = new LinkedHashSet<>();

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "theId")
@LazyCollection(LazyCollectionOption.FALSE)
@BatchSize(size=1000)
@MapKeyColumn(name="attribute_id")
private Map<String, TheAttribute> attributes = new HashMap<>();
我一直试图让hibernate对任何特定示例实例的所有子对象中的所有属性执行单个查询,而不是对实体的每个子对象执行查询。当一次批量加载多个示例实例时,这尤其有益:

@Query("SELECT e FROM Example e where e.theId in (:ids)")
List<Example> findAllByExampleIds(@Param("ids") List<Long> ids);
@Query(“从示例e中选择e,其中e.theId位于(:id)”)
列出findAllByExampleIds(@Param(“ids”)列表ID);

有没有办法通过使用jpa或hibernate注释在hibernate中获得所需的行为?

请在此处查看此问题


您可以在hql查询中使用连接获取来实现这一点

性能是该方法的一个问题。昨晚我只是为孩子们尝试了一下(我无法让孩子们的孩子们工作),发现batchsize方法降低了性能。来自mysql的explain显示了一个“using temporary”,这可能会解释它——我们有一些非常大的表——例如属性表是7.11亿行。
@Query("SELECT e FROM Example e where e.theId in (:ids)")
List<Example> findAllByExampleIds(@Param("ids") List<Long> ids);