Hibernate对孩子们的公共表进行单一查询?

时间:2013-11-26 19:05:38

标签: hibernate

我正在寻找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<>();

一对多映射中的每个类(本例中为ChildObjectOne和ChildObjectTwo)都具有映射的属性,就像在上面的Example类中映射的属性一样

TheAttribute类定义为

@javax.persistence.Entity
@IdClass(TheAttributePK.class)
@Table(name="the_attribute")
public class TheAttribute implements Serializable {

@Id
@Column(name = "the_id", nullable = false)
private long theId;
@Id
@Column(name = "attribute_id", length = 255, nullable = false)
private String attributeId;
...

我一直在尝试做的是让hibernate对任何特定的Example实例的所有子对象的所有属性执行单个查询,而不是为实体的每个子项执行查询。当批量加载多个Example实例时,这尤其有用:

@Query("SELECT e FROM Example e where e.theId in (:ids)")
List<Example> findAllByExampleIds(@Param("ids") List<Long> ids);

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

1 个答案:

答案 0 :(得分:0)

请在这里查看这个问题

FetchMode join makes no difference for ManyToMany relations in spring JPA repositories

您可以在hql查询中使用连接提取来实现此目的。