---
title: "Hibernate Search牛刀小试"
author: "garyli"
date: "2014-01-16T09:48:40Z"
tags: []
categories: ["编程语言"]
slug: "hibernate-search"
draft: false
lastmod: "2014-01-16T09:48:40Z"
id: 2004755
url: "https://garyli.iteye.com/blog/2004755"
---

### [Hibernate Search牛刀小试](http://blog.csdn.net/yanghuw/article/details/1808011)

分类： [Java](http://blog.csdn.net/yanghuw/article/category/170806)2007-09-30 14:16 5300人阅读 [评论](http://blog.csdn.net/yanghuw/article/details/1808011#comments)(4) [收藏]() [举报](http://blog.csdn.net/yanghuw/article/details/1808011#report "举报")

[hibernate](http://www.csdn.net/tag/hibernate)[search](http://www.csdn.net/tag/search)[import](http://www.csdn.net/tag/import)[exception](http://www.csdn.net/tag/exception)[class](http://www.csdn.net/tag/class)[lucene](http://www.csdn.net/tag/lucene)

前几天看到Hibernate与Lucene的整合框架Hiberate Search3.0.0.GA版出来了，昨天试这写了一个Demo，感觉用起来的确很方便的，贴出来与大家分享一下。

1、创建POJO

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package com.yehui;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.CascadeType;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.Column;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.Entity;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.GeneratedValue;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.GenerationType;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.Id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.JoinColumn;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.ManyToOne;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.Table;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.DocumentId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.Field;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.Index;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.Indexed;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.IndexedEmbedded;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.Store;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)/\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * Employee generated by MyEclipse Persistence Tools  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@Entity  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)@Table(name = "employee", catalog = "hise", uniqueConstraints = {})  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@Indexed(index = "indexes/employee")  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)public class Employee implements java.io.Serializable {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static final long serialVersionUID = 7794235365739814541L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private Integer empId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String empName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private Department dept;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String empNo;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private Double empSalary;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    // Constructors  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\** default constructor \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public Employee() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\** minimal constructor \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public Employee(Integer empId) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.empId = empId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\** full constructor \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public Employee(Integer empId, String empName,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)            String empNo, Double empSalary) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.empId = empId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.empName = empName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.empNo = empNo;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.empSalary = empSalary;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    // Property accessors  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Id  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @GeneratedValue(strategy = GenerationType.AUTO)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Column(name = "emp\_id", unique = true, nullable = false, insertable = true, updatable = true)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @DocumentId  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public Integer getEmpId() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return this.empId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setEmpId(Integer empId) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.empId = empId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Column(name = "emp\_name", unique = false, nullable = true, insertable = true, updatable = true, length = 30)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Field(name="name", index=Index.TOKENIZED, store=Store.YES)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public String getEmpName() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return this.empName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setEmpName(String empName) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.empName = empName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Column(name = "emp\_no", unique = false, nullable = true, insertable = true, updatable = true, length = 30)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Field(index=Index.UN\_TOKENIZED)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public String getEmpNo() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return this.empNo;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setEmpNo(String empNo) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.empNo = empNo;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Column(name = "emp\_salary", unique = false, nullable = true, insertable = true, updatable = true, precision = 7)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public Double getEmpSalary() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return this.empSalary;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setEmpSalary(Double empSalary) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.empSalary = empSalary;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @ManyToOne(cascade = CascadeType.ALL)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @JoinColumn(name="dept\_id")  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @IndexedEmbedded(prefix="dept\_", depth=1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public Department getDept() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return dept;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setDept(Department dept) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.dept = dept;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package com.yehui;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.List;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.Column;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.Entity;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.GeneratedValue;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.GenerationType;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.Id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.OneToMany;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.persistence.Table;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.ContainedIn;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.DocumentId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.Field;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.Index;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.Indexed;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.annotations.Store;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)/\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * Department generated by MyEclipse Persistence Tools  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@Entity  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)@Table(name = "department", catalog = "hise", uniqueConstraints = {})  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@Indexed(index="indexes/department")  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)public class Department implements java.io.Serializable {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static final long serialVersionUID = 7891065193118612907L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private Integer deptId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String deptNo;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String deptName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private List&lt;Employee&gt; empList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    // Constructors  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @OneToMany(mappedBy="dept")  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @ContainedIn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public List&lt;Employee&gt; getEmpList() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return empList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setEmpList(List&lt;Employee&gt; empList) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.empList = empList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\** default constructor \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public Department() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\** minimal constructor \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public Department(Integer deptId) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.deptId = deptId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\** full constructor \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public Department(Integer deptId, String deptNo, String deptName) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.deptId = deptId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.deptNo = deptNo;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.deptName = deptName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    // Property accessors  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Id  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @GeneratedValue(strategy=GenerationType.AUTO)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Column(name = "dept\_id", unique = true, nullable = false, insertable = true, updatable = true)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @DocumentId  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public Integer getDeptId() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return this.deptId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setDeptId(Integer deptId) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.deptId = deptId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Column(name = "dept\_no", unique = false, nullable = true, insertable = true, updatable = true, length = 30)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public String getDeptNo() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return this.deptNo;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setDeptNo(String deptNo) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.deptNo = deptNo;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Column(name = "dept\_name", unique = false, nullable = true, insertable = true, updatable = true, length = 30)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Field(name="name", index=Index.TOKENIZED,store=Store.YES)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public String getDeptName() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return this.deptName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setDeptName(String deptName) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.deptName = deptName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

         不了解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、配置文件

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version='1.0' encoding='UTF-8'?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!DOCTYPE hibernate-configuration PUBLIC  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;hibernate-configuration&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;session-factory&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="hibernate.dialect"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        org.hibernate.dialect.MySQLDialect  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="hibernate.connection.url"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        jdbc:mysql://localhost:3306/hise  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="hibernate.connection.password"&gt;123456&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="hibernate.connection.driver\_class"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        com.mysql.jdbc.Driver  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="hibernate.search.default.directory\_provider"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        org.hibernate.search.store.FSDirectoryProvider  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="hibernate.search.default.indexBase"&gt;e:/index&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;mapping class="com.yehui.Employee" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;mapping class="com.yehui.Department" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/session-factory&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/hibernate-configuration&gt;

如果使用JPA，配置文件为

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    http://java.sun.com/xml/ns/persistence/persistence\_1\_0.xsd" version="1.0"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;persistence-unit name="jpaPU" transaction-type="RESOURCE\_LOCAL"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;class&gt;com.yehui.Department&lt;/class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;class&gt;com.yehui.Employee&lt;/class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;properties&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;property name="hibernate.connection.driver\_class"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                value="com.mysql.jdbc.Driver" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;property name="hibernate.connection.url"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                value="jdbc:mysql://localhost:3306/hise" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;property name="hibernate.connection.username" value="root" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;property name="hibernate.connection.password"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                value="123456" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;property name="hibernate.search.default.directory\_provider"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                value="org.hibernate.search.store.FSDirectoryProvider"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;property name="hibernate.search.default.indexBase"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                value="e:/index"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;/properties&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/persistence-unit&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/persistence&gt;

主要就是添加两个属性，hibernate.search.default.directory\_provider指定Directory的代理，即把索引的文件保存在硬盘中（org.hibernate.search.store.FSDirectoryProvider）还是内存里（org.hibernate.search.store.RAMDirectoryProvider），保存在硬盘的话hibernate.search.default.indexBase属性指定索引保存的路径。

3、测试代码

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package com.yehui;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import static junit.framework.Assert.assertNotNull;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import static junit.framework.Assert.assertTrue;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.List;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.lucene.analysis.StopAnalyzer;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.lucene.queryParser.QueryParser;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.Query;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.Session;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.SessionFactory;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.Transaction;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.cfg.AnnotationConfiguration;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.FullTextSession;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.search.Search;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.junit.After;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.junit.Before;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.junit.BeforeClass;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.junit.Test;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)public class SearchResultsHibernate {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static SessionFactory sf = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static Session session = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static Transaction tx = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @BeforeClass  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public static void setupBeforeClass() throws Exception {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        sf = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        assertNotNull(sf);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Before  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setUp() throws Exception {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        session = sf.openSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        tx = session.beginTransaction();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        tx.begin();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @After  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void tearDown() throws Exception {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        tx.commit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        session.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public static void tearDownAfterClass() throws Exception {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if (sf != null)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            sf.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Test  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void testAddDept() throws Exception {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Department dept = new Department();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        dept.setDeptName("Market");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        dept.setDeptNo("6000");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Employee emp = new Employee();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        emp.setDept(dept);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        emp.setEmpName("Kevin");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        emp.setEmpNo("KGP1213");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        emp.setEmpSalary(8000d);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        session.save(emp);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Test  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void testFindAll() throws Exception {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Query query = session.createQuery("from Department");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        List&lt;Department&gt; deptList = query.list();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        assertTrue(deptList.size() &gt; 0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Test  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void testIndex() throws Exception {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FullTextSession fullTextSession = Search.createFullTextSession(session);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        assertNotNull(session);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        QueryParser parser = new QueryParser("name", new StopAnalyzer());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        org.apache.lucene.search.Query luceneQuery = parser  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                .parse("name:Kevin");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Employee.class);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        List list = hibQuery.list();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        assertTrue(list.size() &gt; 0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Test  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void testIndex2() throws Exception {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FullTextSession fullTextSession = Search.createFullTextSession(session);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        assertNotNull(session);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        QueryParser parser = new QueryParser("dept\_name", new StopAnalyzer());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        org.apache.lucene.search.Query luceneQuery = parser  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                .parse("dept\_name:Market");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Employee.class);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        List list = hibQuery.list();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        assertTrue(list.size() &gt; 0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

测试通过。OK