Java Hibernate搜索不起作用
我正在开发一个java/jee应用程序,其中我使用spring boot和hibernate作为框架。我使用hibernate search进行全文搜索,但不幸的是,结果总是得到一个空列表。我使用hibernate版本5.1和hibernate search orm版本5.5.3.Final。以下是我的代码:Java Hibernate搜索不起作用,java,hibernate,spring-boot,hibernate-search,Java,Hibernate,Spring Boot,Hibernate Search,我正在开发一个java/jee应用程序,其中我使用spring boot和hibernate作为框架。我使用hibernate search进行全文搜索,但不幸的是,结果总是得到一个空列表。我使用hibernate版本5.1和hibernate search orm版本5.5.3.Final。以下是我的代码: public void search() { FullTextEntityManager fullTextEntityManager = Search.getFu
public void search() {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(this.em);
try {
fullTextEntityManager.createIndexer().startAndWait();
// create native Lucene query unsing the query DSL
// alternatively you can write the Lucene query using the Lucene
// query parser
// or the Lucene programmatic API. The Hibernate Search DSL is
// recommended though
QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder()
.forEntity(Application.class)
.get();
org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().wildcard().onField("reference").matching("di*")
.createQuery();
// wrap Lucene query in a javax.persistence.Query
javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery,
Application.class);
// execute search
List<Application> result = jpaQuery.getResultList();
System.out.println("your result list is "+result);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
公共作废搜索(){
FullTextEntityManager FullTextEntityManager=Search.getFullTextEntityManager(this.em);
试一试{
fullTextEntityManager.createIndexer().startAndWait();
//使用查询DSL创建本地Lucene查询
//或者,您可以使用Lucene命令编写Lucene查询
//查询解析器
//或者Lucene编程API。Hibernate搜索DSL是
//推荐的
QueryBuilder QueryBuilder=fullTextEntityManager.getSearchFactory()
.buildQueryBuilder()
.forEntity(Application.class)
.get();
org.apache.lucene.search.Query luceneQuery=queryBuilder.keyword().wildcard().onField(“参考”).matching(“di*”)
.createQuery();
//在javax.persistence.query中包装Lucene查询
javax.persistence.Query jpaQuery=fullTextEntityManager.createFullTextQuery(luceneQuery,
应用程序(类别);
//执行搜索
List result=jpaQuery.getResultList();
System.out.println(“您的结果列表是”+结果);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
这是我的实体
package biz.picosoft.entities;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
@Entity
@Indexed
@Table(name = "application",uniqueConstraints = {@UniqueConstraint(columnNames = "reference")})
public class Application implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
@Column(name = "reference")
private String reference;
@Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
@Column(name = "creationDate")
private Date creationDate;
@Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
@Column(name = "status")
private String status;
@Field(index = Index.YES, analyze = Analyze.NO,store = Store.YES)
@Column(name = "deadLine")
private Date deadLine;
@Field(index = Index.YES, analyze = Analyze.NO,store = Store.YES)
@Column(name = "appType")
private String appType;
@Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
@Column(name = "projectId")
private Long projectId;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "App_files", joinColumns = { @JoinColumn(name = "idApp") }, inverseJoinColumns = {
@JoinColumn(name = "idFile") })
private List<FileMetadata> listePiecesJointes = new ArrayList<FileMetadata>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getDeadLine() {
return deadLine;
}
public void setDeadLine(Date deadLine) {
this.deadLine = deadLine;
}
public String getAppType() {
return appType;
}
public void setAppType(String appType) {
this.appType = appType;
}
public Application() {
super();
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public Long getProjectId() {
return projectId;
}
public void setProjectId(Long projectId) {
this.projectId = projectId;
}
public List<FileMetadata> getListePiecesJointes() {
return listePiecesJointes;
}
public void setListePiecesJointes(List<FileMetadata> listePiecesJointes) {
this.listePiecesJointes = listePiecesJointes;
}
public Application(String reference, Date creationDate, String status, Date deadLine, String appType,Long projectId) {
super();
this.reference = reference;
this.creationDate = creationDate;
this.status = status;
this.deadLine = deadLine;
this.appType = appType;
this.projectId=projectId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((appType == null) ? 0 : appType.hashCode());
result = prime * result + ((creationDate == null) ? 0 : creationDate.hashCode());
result = prime * result + ((deadLine == null) ? 0 : deadLine.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((projectId == null) ? 0 : projectId.hashCode());
result = prime * result + ((reference == null) ? 0 : reference.hashCode());
result = prime * result + ((status == null) ? 0 : status.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Application other = (Application) obj;
if (appType == null) {
if (other.appType != null)
return false;
} else if (!appType.equals(other.appType))
return false;
if (creationDate == null) {
if (other.creationDate != null)
return false;
} else if (!creationDate.equals(other.creationDate))
return false;
if (deadLine == null) {
if (other.deadLine != null)
return false;
} else if (!deadLine.equals(other.deadLine))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (projectId == null) {
if (other.projectId != null)
return false;
} else if (!projectId.equals(other.projectId))
return false;
if (reference == null) {
if (other.reference != null)
return false;
} else if (!reference.equals(other.reference))
return false;
if (status == null) {
if (other.status != null)
return false;
} else if (!status.equals(other.status))
return false;
return true;
}
@Override
public String toString() {
return "Application [id=" + id + ", reference=" + reference + ", creationDate=" + creationDate + ", status="
+ status + ", deadLine=" + deadLine + ", appType=" + appType + ", projectId=" + projectId + "]";
}
}
package biz.picosoft.entities;
导入java.io.Serializable;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入javax.persistence.CascadeType;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.JoinColumn;
导入javax.persistence.JoinTable;
导入javax.persistence.OneToMany;
导入javax.persistence.Table;
导入javax.persistence.UniqueConstraint;
导入org.hibernate.search.annotations.Analyze;
导入org.hibernate.search.annotations.Field;
导入org.hibernate.search.annotations.Index;
导入org.hibernate.search.annotations.index;
导入org.hibernate.search.annotations.Store;
@实体
@索引
@表(name=“application”,uniqueConstraints={@UniqueConstraint(columnNames=“reference”)})
公共类应用程序实现可序列化{
/**
*
*/
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
@列(name=“id”)
私人长id;
@字段(index=index.YES,analyze=analyze.NO,store=store.YES)
@列(name=“reference”)
私有字符串引用;
@字段(index=index.YES,analyze=analyze.NO,store=store.YES)
@列(name=“creationDate”)
私人约会;
@字段(index=index.YES,analyze=analyze.NO,store=store.YES)
@列(name=“status”)
私有字符串状态;
@字段(index=index.YES,analyze=analyze.NO,store=store.YES)
@列(name=“截止日期”)
私人约会截止日期;
@字段(index=index.YES,analyze=analyze.NO,store=store.YES)
@列(name=“appType”)
私有字符串appType;
@字段(index=index.YES,analyze=analyze.NO,store=store.YES)
@列(name=“projectId”)
私人长投射;
@OneToMany(级联=级联类型.ALL)
@JoinTable(name=“App_files”,joinColumns={@JoinColumn(name=“idApp”)},inverseJoinColumns={
@JoinColumn(name=“idFile”)}
private List ListPiecessPointes=new ArrayList();
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公开日期getCreationDate(){
还肌酐;
}
公共无效设置创建日期(创建日期){
this.creationDate=creationDate;
}
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公开日期(截止日期){
归还期限;
}
公开作废设置截止日期(日期截止日期){
this.deadLine=截止日期;
}
公共字符串getAppType(){
返回appType;
}
公共void setAppType(字符串appType){
this.appType=appType;
}
公共应用程序(){
超级();
}
公共字符串getReference(){
返回参考;
}
公共void集合引用(字符串引用){
this.reference=参考;
}
公共长getProjectId(){
返回投影;
}
公共void setProjectId(长projectId){
this.projectId=projectId;
}
公共列表GetListPiecessPoints(){
返回所列项目点;
}
public void setListPiecesJ点(列出ListPiecesJ点){
this.listpoisesjiones=listpoisesjiones;
}
公共应用程序(字符串引用、日期创建日期、字符串状态、日期截止日期、字符串appType、长项目ID){
超级();
this.reference=参考;
this.creationDate=creationDate;
这个状态=状态;
this.deadLine=截止日期;
this.appType=appType;
this.projectId=projectId;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+((appType==null)?0:appType.hashCode();
result=prime*result+((creationDate==null)?0:creationDate.hashCode();
result=prime*result+((deadLine==null)?0:deadLine.hashCode());
结果=素数*结果