Hibernate Search uses annotations to map entities to a Lucene index, check the reference documentation for more informations.
Hibernate Search使用Annotations将实体与Lucene索引,具体的信息请参考相关文档。
Hibernate Search is fully integrated with the API and semantic of JPA / Hibernate. Switching from a HQL or Criteria based query requires just a few lines of code. The main API the application interacts with is the FullTextSession
API (subclass of Hibernate's Session
).
Hibernate Search能够完美地融合到JPA/Hibernate的API和语法里面。从HQL或者标准的查询语句切换到Hibernate Search,只需要短短几行代码。程序中用到的主要API为FullTextSession API(Hibernate Session的子类)。
When Hibernate Search is present, JBoss Seam injects a FullTextSession
.
当使用Hibernate Search的时候,需要使用JBoss Seam注入一个FullTextSession。
@Stateful
@Name("search")
public class FullTextSearchAction implements FullTextSearch, Serializable {
@In FullTextSession session;
public void search(String searchString) {
org.apache.lucene.search.Query luceneQuery = getLuceneQuery();
org.hibernate.Query query session.createFullTextQuery(luceneQuery, Product.class);
searchResults = query
.setMaxResults(pageSize + 1)
.setFirstResult(pageSize * currentPage)
.list();
}
[...]
}
注意
FullTextSession
extends org.hibernate.Session
so that it can be used as a regular Hibernate Session
FullTextSession是继承自org.hibernate.Session,所以,它完全可以当作一个标准的Hibernate Session来使用。
If the Java Persistence API is used, a smoother integration is proposed.
如果使用Java Persistence API,那么集成的工作更加完善。
@Stateful
@Name("search")
public class FullTextSearchAction implements FullTextSearch, Serializable {
@In FullTextEntityManager em;
public void search(String searchString) {
org.apache.lucene.search.Query luceneQuery = getLuceneQuery();
javax.persistence.Query query = em.createFullTextQuery(luceneQuery, Product.class);
searchResults = query
.setMaxResults(pageSize + 1)
.setFirstResult(pageSize * currentPage)
.getResultList();
}
[...]
}
When Hibernate Search is present, a FulltextEntityManager
is injected. FullTextEntityManager
extends EntityManager
with search specific methods, the same way FullTextSession
extends Session
.
当使用Hibernate Search的时候,会注入一个FulltextEntityManager。FullTextEntityManager继承自EntityManager,情况与FullTextSession继承自Session类似。
When an EJB 3.0 Session or Message Driven Bean injection is used (i.e. via the @PersistenceContext annotation), it is not possible to replace the EntityManager
interface by the FullTextEntityManager
interface in the declaration statement. However, the implementation injected will be a FullTextEntityManager
implementation: downcasting is then possible.
当使用注入的EJB 3.0 Session或者Message Driven Bean的时候(例如通过@PersistenceContext annotation),那么连使用FullTextEntityManager来替代EntityManager都不用。不过实际被注入的仍然是FullTextEntityManager,所以可以在后面去强制转换。
@Stateful
@Name("search")
public class FullTextSearchAction implements FullTextSearch, Serializable {
@PersistenceContext EntityManager em;
public void search(String searchString) {
org.apache.lucene.search.Query luceneQuery = getLuceneQuery();
FullTextEntityManager ftEm = (FullTextEntityManager) em;
javax.persistence.Query query = ftEm.createFullTextQuery(luceneQuery, Product.class);
searchResults = query
.setMaxResults(pageSize + 1)
.setFirstResult(pageSize * currentPage)
.getResultList();
}
[...]
}
小心
For people accustomed to Hibernate Search out of Seam, note that using Search.createFullTextSession
is not necessary.
如果你还没有在Seam中使用过Hibernate Search,那么需要注意,你不再需要使用Search.createFullTextSession。
Check the DVDStore or the blog examples of the JBoss Seam distribution for a concrete use of Hibernate Search.
Seam示例中的DVDStore或者blog示例都使用到了Hibernate Search,具体的应用请参考这两个示例。
相关推荐
##### 1.2 第二部分:映射关联 - **映射 Person 类**:展示如何在映射文件中定义实体间的关联关系。 - **单向 Set 基础关联**:讲解单向一对多关联的实现方法。 - **操作关联**:演示如何通过代码操纵实体间的关联。...
1.2. 第一个例子:注册示例.............................................................................................................................................. 15 1.2.1. 了解代码.................
接下来第二步安装过程中设置系统参数,系统会自动检测出一些配置项,请根据您的实际情况填写,如图二所示,数据库密码不能为空。 图二:系统参数设置 点击提交后进入第三步进入最后的系统安装过程,安装过程可能...
28. 使用SQL数据库 i. 28.1. 配置DataSource i. 28.1.1. 对内嵌数据库的支持 ii. 28.1.2. 连接到一个生产环境数据库 iii. 28.1.3. 连接到一个JNDI数据库 ii. 28.2. 使用JdbcTemplate iii. 28.3. JPA和Spring Data i...
28个目标文件 内容索引:JAVA源码,媒体网络,飞鸽传书 Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java文件传输实例不可错过,Java网络...
28个目标文件 内容索引:JAVA源码,媒体网络,飞鸽传书 Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java文件传输实例不可错过,Java网络...
28个目标文件 内容索引:JAVA源码,媒体网络,飞鸽传书 Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java文件传输实例不可错过,Java网络...
28个目标文件 内容索引:JAVA源码,媒体网络,飞鸽传书 Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java文件传输实例不可错过,Java网络...
Java局域网通信——飞鸽传书源代码 28个目标文件 内容索引:JAVA源码,媒体网络,飞鸽传书 Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java...
Java局域网通信——飞鸽传书源代码 28个目标文件 内容索引:JAVA源码,媒体网络,飞鸽传书 Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java...