Hibernate搜索不起作用

时间:2018-05-15 13:31:59

标签: java hibernate spring-boot hibernate-search

我正在开发一个java / jee应用程序,其中我使用spring引导和hibernate作为框架我使用hibernate搜索全文搜索但不幸的是我总是得到一个空列表结果。我正在使用hibernate版本5.1和hibernate搜索orm version 5.5.3.Final.Here是我的代码:

 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 + "]";
    }




}

2 个答案:

答案 0 :(得分:0)

在搜索之前,您确定要对您的实体编制索引吗?

如果您正在使用预先存在的数据库,或者在不使用Hibernate ORM API(通过直接使用SQL或通过恢复转储)初始化数据库,则您的实体可能尚未编入索引。

您应该查看群发索引器,这样可以轻松索引很多实体:https://docs.jboss.org/hibernate/search/5.5/reference/en-US/html_single/#search-batchindex-massindexer

答案 1 :(得分:0)

我已经解决了我的问题。这是因为没有索引的子类。

相关问题