Java Dropwizard休眠搜索

Java Dropwizard休眠搜索,java,hibernate,dropwizard,Java,Hibernate,Dropwizard,我正忙于一个Dropwizard应用程序,想在我的数据库中搜索某些实体。我已经看到,这可以通过在实体类的顶部创建NamedQuery语句来实现,如中所示。然后从DAO类执行查询 后来我偶然发现了它,在数据库中搜索实体时,它似乎是一个更好的解决方案。因此,我的问题是Dropwizard框架是否支持它?此外,如果支持它,将如何配置和使用它?我在这本书里找不到它的任何参考资料 通过反复试验,我终于找到了如何在Dropwizard应用程序中使用hibernate搜索的方法。事实证明,Dropwizard

我正忙于一个
Dropwizard
应用程序,想在我的数据库中搜索某些实体。我已经看到,这可以通过在
实体
类的顶部创建
NamedQuery
语句来实现,如中所示。然后从DAO类执行查询


后来我偶然发现了它,在数据库中搜索实体时,它似乎是一个更好的解决方案。因此,我的问题是Dropwizard框架是否支持它?此外,如果支持它,将如何配置和使用它?我在这本书里找不到它的任何参考资料

通过反复试验,我终于找到了如何在Dropwizard应用程序中使用hibernate搜索的方法。事实证明,Dropwizard不直接支持它,但只需稍加努力即可添加。我做了以下工作:

1.在我的pom文件中添加了Hibernate搜索依赖项:

    <!-- Search -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search-orm</artifactId>
        <version>5.9.0.Final</version>
    </dependency>
3.使我的实体类可以使用
@索引
@字段
注释进行搜索

@Entity
@Indexed
@Table(name = "product")
public class Product {

    @Id
    private int id;

    @Field(termVector = TermVector.YES)
    private String productName;

    @Field(termVector = TermVector.YES)
    private String description;

    @Field
    private int memory;

    // getters, setters, and constructors
} 
4.然后在我的DAO类中,我可以如下搜索实体:

public List<Product> search(String term) {
        EntityManager em = this.currentSession();

        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);

        // 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 qb = fullTextEntityManager.getSearchFactory()
                .buildQueryBuilder()
                .forEntity(Product.class)
                .get();

        org.apache.lucene.search.Query luceneQuery = qb
                .keyword()
                .onFields("description",  "productName")
                .matching(term)
                .createQuery();

        // wrap Lucene query in a javax.persistence.Query
        javax.persistence.Query jpaQuery =
                fullTextEntityManager.createFullTextQuery(luceneQuery, Product.class);

        // execute search
        List<Product> result = jpaQuery.getResultList();

        return result;
    }
公共列表搜索(字符串术语){
EntityManager em=this.currentSession();
FullTextEntityManager FullTextEntityManager=Search.getFullTextEntityManager(em);
//使用查询DSL创建本地Lucene查询
//或者,您可以使用Lucene查询解析器编写Lucene查询
//或者Lucene编程API。不过建议使用Hibernate搜索DSL
QueryBuilder qb=fullTextEntityManager.getSearchFactory()
.buildQueryBuilder()
.forEntity(产品类别)
.get();
org.apache.lucene.search.Query luceneQuery=qb
.keyword()
.onFields(“说明”、“产品名称”)
.配对(学期)
.createQuery();
//在javax.persistence.query中包装Lucene查询
javax.persistence.Query jpaQuery=
createFullTextQuery(luceneQuery,Product.class);
//执行搜索
List result=jpaQuery.getResultList();
返回结果;
}

我发现在我的实施过程中非常有用。

您特别需要什么支持?如果DAO的extend DW的
AbstractDAO
,您可以访问hibernate会话,并使用
search.getFullTextSession(会话)
获取搜索会话。我想知道Dropwizard中是否包含hibernate搜索功能,即依赖项是否存在,如果存在,应该如何配置和使用它?我似乎找不到关于如何做到这一点的教程或示例。
public List<Product> search(String term) {
        EntityManager em = this.currentSession();

        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);

        // 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 qb = fullTextEntityManager.getSearchFactory()
                .buildQueryBuilder()
                .forEntity(Product.class)
                .get();

        org.apache.lucene.search.Query luceneQuery = qb
                .keyword()
                .onFields("description",  "productName")
                .matching(term)
                .createQuery();

        // wrap Lucene query in a javax.persistence.Query
        javax.persistence.Query jpaQuery =
                fullTextEntityManager.createFullTextQuery(luceneQuery, Product.class);

        // execute search
        List<Product> result = jpaQuery.getResultList();

        return result;
    }