简体   繁体   English

如何使用QueryDSL构建GenericDao?

[英]How do I build a GenericDao using QueryDSL?

First of, I am really new to QueryDSL. 首先,我是QueryDSL的新手。

I'm using a Spring + Hibernate environment. 我正在使用Spring + Hibernate环境。

The problem I'm facing is with building a GenericDAO to implement all the basic CRUD operations, but I'm not sure how do I get the static reference from a QEntity. 我面临的问题是构建GenericDAO来实现所有基本的CRUD操作,但我不知道如何从QEntity获取静态引用。

My entity class structure looks like this: 我的实体类结构如下所示:

    @Entity //jpa
    public class Entity extends AbstractEntity{
    //fields
    ...
    }

    public abstract class AbstractEntity{
    //Logger 
    }

The basic structure of a QueryDSL-generated entity QueryDSL生成的实体的基本结构

    public class QEntity extends PEntity<Entity>{
    ...
    public static final QEntity entity = new QEntity("entity");        
    ...
    //constructors

    } 

And the GenericDao would look like this: GenericDao看起来像这样:

    public class abstract GenericDao<T extends AbstractEntity, K extends PEntity<? extends AbstractEntity>>{

    //some kind of method to get the K.k (QEntity.entity) reference.        
    //CRUD operations using T and K

    }

One approach would be using Reflection, but I'm not an advocate of using that method, especially in this case. 一种方法是使用Reflection,但我不是使用该方法的倡导者,尤其是在这种情况下。

Another thing that I'm not sure of, is it mandatory to use the static reference from a QEntity to build queries or is it ok if I do a contructor call to get a new object. 另一件我不确定的事情是,是否必须使用QEntity中的静态引用来构建查询,或者如果我执行构造函数调用以获取新对象,则可以。 Also, what does the String in the constructor parameter signify? 另外,构造函数参数中的String表示什么?

    public QEntity(String variable) {
    this(Entity.class, forVariable(variable), INITS);
    }

The problem I'm facing is with building a GenericDAO to implement all the basic CRUD operations, but I'm not sure how do I get the static reference from a QEntity. 我面临的问题是构建GenericDAO来实现所有基本的CRUD操作,但我不知道如何从QEntity获取静态引用。

Without a reference to the QEntity class it is difficult, so make sure you provide an instance to it to your DAO. 如果没有QEntity类的引用则很难,所以请确保为DAO提供实例。

Another thing that I'm not sure of, is it mandatory to use the static reference from a QEntity to build queries or is it ok if I do a contructor call to get a new object. 另一件我不确定的事情是,是否必须使用QEntity中的静态引用来构建查询,或者如果我执行构造函数调用以获取新对象,则可以。 Also, what does the String in the constructor parameter signify? 另外,构造函数参数中的String表示什么?

No, it's not mandatory, it's a convenience instance. 不,这不是强制性的,它是一个方便的例子。 The constructor argument is the variable name. 构造函数参数是变量名称。 If you provide a custom instance, make sure that you use the same variable name consistently. 如果提供自定义实例,请确保始终使用相同的变量名称。

Also make sure that you use the latest Querydsl version. 还要确保使用最新的Querydsl版本。 PEntity looks like a pre-2.0 class. PEntity看起来像2.0之前的类。

Here is an example of a generic DAO superclass for Querydsl JPA usage https://github.com/querydsl/querydsl/blob/master/querydsl-examples/querydsl-example-jpa-guice/src/main/java/com/querydsl/example/jpa/repository/AbstractRepository.java 以下是Querydsl JPA用法的通用DAO超类的示例https://github.com/querydsl/querydsl/blob/master/querydsl-examples/querydsl-example-jpa-guice/src/main/java/com/querydsl /example/jpa/repository/AbstractRepository.java

Update 更新

If you want to avoid passing the Q-type to your DAO class you can use a pattern like this https://github.com/spring-projects/spring-data-jpa/blob/master/src/main/java/org/springframework/data/jpa/repository/support/QueryDslRepositorySupport.java#L54 如果你想避免将Q-type传递给你的DAO类,你可以使用这样的模式https://github.com/spring-projects/spring-data-jpa/blob/master/src/main/java/org /springframework/data/jpa/repository/support/QueryDslRepositorySupport.java#L54

The variable name will be the simple name of you entity class with the first letter converted to lowercase. 变量名称将是您实体类的简单名称,第一个字母转换为小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM