【发布时间】:2013-12-12 02:39:07
【问题描述】:
我希望让 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)的属性映射与上述示例类中映射的属性完全相同
Attribute 类的定义如下
@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 实例的所有子对象的所有属性执行单个查询,而不是为实体的每个子对象执行查询。这在一次批量加载多个示例实例时尤其有用:
@Query("SELECT e FROM Example e where e.theId in (:ids)")
List<Example> findAllByExampleIds(@Param("ids") List<Long> ids);
有没有办法使用 jpa 或 hibernate 注释通过 hibernate 获得这种所需的行为?
【问题讨论】:
标签: hibernate