我希望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 )都映射了属性,与上面示例类中映射的属性完全相同
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对任何特定示例实例的所有子对象的所有属性执行一次查询,而不是为一个或多个实体的每个子对象执行查询。当一次批量加载多个示例实例时,这将特别有用:
@Query("SELECT e FROM Example e where e.theId in (:ids)")
List<Example> findAllByExampleIds(@Param("ids") List<Long> ids);有没有办法使用jpa或hibernate注解在hibernate中获得这种期望的行为?
发布于 2013-11-27 04:11:44
请看这里的这个问题
FetchMode join makes no difference for ManyToMany relations in spring JPA repositories
您可以在hql查询中使用join fetch来实现这一点。
https://stackoverflow.com/questions/20225792
复制相似问题