在休眠搜索中使用一个查询获取具有关联的实体
Fetch entity with association using one query in hibernate search
我是 Hibernate Search 项目的新手,所以任何建议都会被采纳。假设我有一个实体 Foo 和实体 Bar 以一对多关系连接。映射可能如下所示:
@Entity
@Table(name="foos")
@Indexed
public class Foo {
@Id
private Long id;
@IndexedEmbedded
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "bar_id")
private Bar bar;
//getters, setters, etc.
}
@Entity
@Table(name = "bars")
public class Bar {
@Id
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy="bar")
private Set<Foo> fooSet;
//getters, setters, etc.
}
现在,当我尝试使用 lucene/hibernate 搜索 dsl 查询 Foo table 时,我得到如下查询结果:
select this_ from foos this_ where this_.id in (id collection fetched from lucene)
所以我的 Bar 实体由于延迟获取类型而被休眠代理。我的问题是有没有一种方法可以使用一个查询(使用连接或其他方式)获取 Foo 和 Bar?
我终于找到了解决办法。我们需要使用 FullTextQuery 接口中的 setCriteriaQuery(Criteria) 方法。来自 javadoc:
Defines the Database Query used to load the Lucene results. Useful to load a given object graph by refining the fetch modes No projection (criteria.setProjection() ) allowed, the root entity must be the only returned type No where restriction can be defined either
所以,解决方案如下所示:
FullTextQuery myQuery = ... //setup my lucene query here
Criteria fetchAssociationCriteria = session.createCriteria(Foo.class);
fetchAssociationCriteria.setFetchMode("bar", FetchMode.JOIN);
List<Foo> foos = myQuery.setCriteriaQuery(fetchAssociationCriteria).getResultList();
产生如下查询:
select (foo and bar attributes) from foos this_ left outer join bar bars2_ where this.id in (id collection fetched from lucene)
给 JPA 用户的提示:
FullTextQuery 接口支持 JPA,但您需要将 Hibernate 的条件查询传递给 setCriteriaQuery() 方法。要获取 Hibernate 的会话,请使用 EntityManager 的 unwrap 方法。
Session session = entityManager.unwrap(Session.class);
我是 Hibernate Search 项目的新手,所以任何建议都会被采纳。假设我有一个实体 Foo 和实体 Bar 以一对多关系连接。映射可能如下所示:
@Entity
@Table(name="foos")
@Indexed
public class Foo {
@Id
private Long id;
@IndexedEmbedded
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "bar_id")
private Bar bar;
//getters, setters, etc.
}
@Entity
@Table(name = "bars")
public class Bar {
@Id
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy="bar")
private Set<Foo> fooSet;
//getters, setters, etc.
}
现在,当我尝试使用 lucene/hibernate 搜索 dsl 查询 Foo table 时,我得到如下查询结果:
select this_ from foos this_ where this_.id in (id collection fetched from lucene)
所以我的 Bar 实体由于延迟获取类型而被休眠代理。我的问题是有没有一种方法可以使用一个查询(使用连接或其他方式)获取 Foo 和 Bar?
我终于找到了解决办法。我们需要使用 FullTextQuery 接口中的 setCriteriaQuery(Criteria) 方法。来自 javadoc:
Defines the Database Query used to load the Lucene results. Useful to load a given object graph by refining the fetch modes No projection (criteria.setProjection() ) allowed, the root entity must be the only returned type No where restriction can be defined either
所以,解决方案如下所示:
FullTextQuery myQuery = ... //setup my lucene query here
Criteria fetchAssociationCriteria = session.createCriteria(Foo.class);
fetchAssociationCriteria.setFetchMode("bar", FetchMode.JOIN);
List<Foo> foos = myQuery.setCriteriaQuery(fetchAssociationCriteria).getResultList();
产生如下查询:
select (foo and bar attributes) from foos this_ left outer join bar bars2_ where this.id in (id collection fetched from lucene)
给 JPA 用户的提示:
FullTextQuery 接口支持 JPA,但您需要将 Hibernate 的条件查询传递给 setCriteriaQuery() 方法。要获取 Hibernate 的会话,请使用 EntityManager 的 unwrap 方法。
Session session = entityManager.unwrap(Session.class);