Я разрабатываю приложение java / jee, в котором я использую весеннюю загрузку и спящий режим в качестве фреймворков. Я использовал поиск в спящем режиме для полнотекстового поиска, но, к сожалению, в результате я всегда получаю пустой список. orm версия 5.5.3. Наконец. Вот мой код:
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();
}
}
и вот моя сущность
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 + "]";
}
}