前几天看到Hibernate与Lucene的整合框架Hiberate Search3.0.0.GA版出来了,昨天试这写了一个Demo,感觉用起来的确很方便的,贴出来与大家分享一下。
1、创建POJO
java 代码
- @Entity
- @Table(name = "employee", catalog = "hise", uniqueConstraints = {})
- @Indexed(index = "indexes/employee")
- public class Employee implements java.io.Serializable {
- private static final long serialVersionUID = 7794235365739814541L;
- private Integer empId;
- private String empName;
- private Department dept;
- private String empNo;
- private Double empSalary;
-
-
-
-
- public Employee() {
- }
-
-
- public Employee(Integer empId) {
- this.empId = empId;
- }
-
-
- public Employee(Integer empId, String empName,
- String empNo, Double empSalary) {
- this.empId = empId;
- this.empName = empName;
- this.empNo = empNo;
- this.empSalary = empSalary;
- }
-
-
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- @Column(name = "emp_id", unique = true, nullable = false, insertable = true, updatable = true)
- @DocumentId
- public Integer getEmpId() {
- return this.empId;
- }
-
- public void setEmpId(Integer empId) {
- this.empId = empId;
- }
-
- @Column(name = "emp_name", unique = false, nullable = true, insertable = true, updatable = true, length = 30)
- @Field(name="name", index=Index.TOKENIZED, store=Store.YES)
- public String getEmpName() {
- return this.empName;
- }
-
- public void setEmpName(String empName) {
- this.empName = empName;
- }
-
- @Column(name = "emp_no", unique = false, nullable = true, insertable = true, updatable = true, length = 30)
- @Field(index=Index.UN_TOKENIZED)
- public String getEmpNo() {
- return this.empNo;
- }
-
- public void setEmpNo(String empNo) {
- this.empNo = empNo;
- }
-
- @Column(name = "emp_salary", unique = false, nullable = true, insertable = true, updatable = true, precision = 7)
- public Double getEmpSalary() {
- return this.empSalary;
- }
-
- public void setEmpSalary(Double empSalary) {
- this.empSalary = empSalary;
- }
-
- @ManyToOne(cascade = CascadeType.ALL)
- @JoinColumn(name="dept_id")
- @IndexedEmbedded(prefix="dept_")
- public Department getDept() {
- return dept;
- }
-
- public void setDept(Department dept) {
- this.dept = dept;
- }
- }
java 代码
- @Entity
- @Table(name = "department", catalog = "hise", uniqueConstraints = {})
- @Indexed(index="indexes/department")
- public class Department implements java.io.Serializable {
- private static final long serialVersionUID = 7891065193118612907L;
- private Integer deptId;
- private String deptNo;
- private String deptName;
- private List<Employee> empList;
-
-
-
- @OneToMany(mappedBy="dept")
- @ContainedIn
- public List<Employee> getEmpList() {
- return empList;
- }
-
- public void setEmpList(List<Employee> empList) {
- this.empList = empList;
- }
-
-
- public Department() {
- }
-
-
- public Department(Integer deptId) {
- this.deptId = deptId;
- }
-
-
- public Department(Integer deptId, String deptNo, String deptName) {
- this.deptId = deptId;
- this.deptNo = deptNo;
- this.deptName = deptName;
- }
-
-
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- @Column(name = "dept_id", unique = true, nullable = false, insertable = true, updatable = true)
- @DocumentId
- public Integer getDeptId() {
- return this.deptId;
- }
-
- public void setDeptId(Integer deptId) {
- this.deptId = deptId;
- }
-
- @Column(name = "dept_no", unique = false, nullable = true, insertable = true, updatable = true, length = 30)
- public String getDeptNo() {
- return this.deptNo;
- }
-
- public void setDeptNo(String deptNo) {
- this.deptNo = deptNo;
- }
-
- @Column(name = "dept_name", unique = false, nullable = true, insertable = true, updatable = true, length = 30)
- @Field(name="name", index=Index.TOKENIZED,store=Store.YES)
- public String getDeptName() {
- return this.deptName;
- }
-
- public void setDeptName(String deptName) {
- this.deptName = deptName;
- }
- }
不了解Hibernate映射相关的Annotation的朋友可以到Hibernate的官方网站下载Hibernate Annotation Reference,有http://wiki.redsaga.com/翻译的中文文档。当然,也可以直接使用hbm.xml文件。
Hibernate Search相关的Annotation主要有两个:
@Indexed 标识需要进行索引的对象,
属性 index 指定索引文件的路径
@Field 标注在类的get属性上,标识一个索引的Field
属性 index 指定是否索引,与Lucene相同
store 指定是否索引,与Lucene相同
name 指定Field的name,默认为类属性的名称
analyzer 指定分析器
另外@IndexedEmbedded 与 @ContainedIn 用于关联类之间的索引
@IndexedEmbedded有两个属性,一个prefix指定关联的前缀,一个depth指定关联的深度
如上面两个类中Department类可以通过部门名称name来索引部门,在Employee与部门关联的前缀为dept_,因此可以通过部门名称dept_name来索引一个部门里的所有员工。
2、配置文件
xml 代码
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
-
- <hibernate-configuration>
-
- <session-factory>
- <property name="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="hibernate.connection.url">
- jdbc:mysql://localhost:3306/hise
- </property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">123456</property>
- <property name="hibernate.connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
-
- <property name="hibernate.search.default.directory_provider">
- org.hibernate.search.store.FSDirectoryProvider
- </property>
- <property name="hibernate.search.default.indexBase">e:/index</property>
-
- <mapping class="com.yehui.Employee" />
- <mapping class="com.yehui.Department" />
- </session-factory>
-
- </hibernate-configuration>
如果使用JPA,配置文件为
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence xmlns="http://java.sun.com/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
- http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
-
- <persistence-unit name="jpaPU" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <class>com.yehui.Department</class>
- <class>com.yehui.Employee</class>
- <properties>
- <property name="hibernate.connection.driver_class"
- value="com.mysql.jdbc.Driver" />
- <property name="hibernate.connection.url"
- value="jdbc:mysql://localhost:3306/hise" />
- <property name="hibernate.connection.username" value="root" />
- <property name="hibernate.connection.password"
- value="123456" />
- <property name="hibernate.search.default.directory_provider"
- value="org.hibernate.search.store.FSDirectoryProvider"/>
- <property name="hibernate.search.default.indexBase"
- value="e:/index"/>
- </properties>
- </persistence-unit>
-
- </persistence>
主要就是添加两个属性,
hibernate.search.default.directory_provider指定Directory的代理,即把索引的文件保存在硬盘中(org.hibernate.search.store.FSDirectoryProvider)还是内存里(org.hibernate.search.store.RAMDirectoryProvider),保存在硬盘的话hibernate.search.default.indexBase属性指定索引保存的路径。
3、测试代码
java 代码
- public class SearchResultsHibernate {
- private static SessionFactory sf = null;
- private static Session session = null;
- private static Transaction tx = null;
-
- @BeforeClass
- public static void setupBeforeClass() throws Exception {
- sf = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
-
- assertNotNull(sf);
- }
-
- @Before
- public void setUp() throws Exception {
- session = sf.openSession();
- tx = session.beginTransaction();
- tx.begin();
- }
-
- @After
- public void tearDown() throws Exception {
- tx.commit();
- session.close();
- }
-
- public static void tearDownAfterClass() throws Exception {
- if (sf != null)
- sf.close();
- }
-
- @Test
- public void testAddDept() throws Exception {
- Department dept = new Department();
- dept.setDeptName("Market");
- dept.setDeptNo("6000");
-
- Employee emp = new Employee();
- emp.setDept(dept);
- emp.setEmpName("Kevin");
- emp.setEmpNo("KGP1213");
- emp.setEmpSalary(8000d);
-
- session.save(emp);
- }
-
- @Test
- public void testFindAll() throws Exception {
- Query query = session.createQuery("from Department");
-
- List<Department> deptList = query.list();
-
- assertTrue(deptList.size() > 0);
- }
-
- @Test
- public void testIndex() throws Exception {
- FullTextSession fullTextSession = Search.createFullTextSession(session);
- assertNotNull(session);
-
- QueryParser parser = new QueryParser("name", new StopAnalyzer());
- org.apache.lucene.search.Query luceneQuery = parser
- .parse("name:Kevin");
- Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery,
- Employee.class);
-
- List list = hibQuery.list();
- assertTrue(list.size() > 0);
- }
- }
测试通过。OK
分享到:
相关推荐
Hibernate Search is a library providing full-text search capabilities to Hibernate. It opens doors to more human friendly and efficient search engines while still following the Hibernate and Java ...
Hibernate Search的作用是对数据库中的数据进行检索的。它是hibernate对著名的全文检索系统Lucene的一个集成方案,作用在于对数据表中某些内容庞大的字段(如声明为text的字段)建立全文索引,这样通过hibernate ...
hibernate Search in action
hibernate-search, Hibernate Search Hibernate 搜索版本:5.8.0. Final - 13-09-2017描述针对Java对象的全文搜索这个项目提供 Hibernate ORM和全文索引服务( 如 Apache Lucene和 Elasticsearch
hibernate search全文索引的创建与查询实例 hibernate-search-analyzers-4.1.1.Final.jar
基于Spring的Hibernate Search全文检索功能示例 实例下载
Hibernate Search 4.4.0.Final API 帮助文档
hibernate search 里面有关于代码的详细说明 请看readme txt 有相关jar直接运行
HibernateSearch的使用方法
hibernate search 开发环境的搭建。
使用hibernate search实现全文检索和文档管理功能: 1 全文检索 2 手动生成索引 3 文档上传(自动建立索引) 4 文档更新(自动建立索引) 5 文档删除 使用说明: 1 需要先恢复数据库 searchDB_2008sqlserver.bak 2 ...
Hibernate Search in Action introduces both the principles of enterprise search and the implementation details a Java developer will need to use Hibernate Search effectively. This book blends the ...
压缩包包括项目完整代码,详细说明和项目结构图,效果图 项目实现了分页和高亮显示 MAVEN项目:HibernateSearchDemo IDE :MyEclipse jdk :1.6 数据库 :MySql ...hibernate search 版本 4.4.1.Final
NULL 博文链接:https://wjy320.iteye.com/blog/2007060
Hibernate Search in action (pdf && code)
hibernate-search-3.4.1 api chm
hibernate_search.pdf
NULL 博文链接:https://dreamzhong.iteye.com/blog/1213882