How to create GenericDao using QueryDSL?

First off, I'm really new to QueryDSL.

I am using Spring + Hibernate environment.

The problem I ran into is creating a GenericDAO to implement all the basic CRUD operations, but I'm not sure how to get a static link from QEntity.

My entity class structure is as follows:

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

The basic structure of an object created by QueryDSL

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

And GenericDao will look like this:

  public class abstract GenericDao<T extends AbstractEntity, K extends PEntity<? extends AbstractEntity>>{ //some kind of method to get the Kk (QEntity.entity) reference. //CRUD operations using T and K } 

One approach would be to use Reflection, but I am not a proponent of using this method, especially in this case.

Another thing I'm not sure about is the mandatory use of a static link from QEntity to create queries, or is it ok if I make a contructor call to get a new object. Also, what does the string in the constructor parameter mean?

  public QEntity(String variable) { this(Entity.class, forVariable(variable), INITS); } 
+8
java generics jpa dao querydsl
source share
1 answer

The problem I ran into is creating a GenericDAO to implement all the basic CRUD operations, but I'm not sure how to get a static link to QEntity.

Without a reference to the QEntity class, this is difficult, so make sure you provide it with an instance for your DAO.

Another thing I'm not sure about is the mandatory use of a static QEntity link to create queries, or is it ok if I make a contructor call to get a new object. Also, what does a string in a constructor parameter mean?

No, this is not necessary, it is a convenient instance. The constructor argument is the name of the variable. If you provide your own instance, make sure that you use the same variable name.

Also make sure you are using the latest version of Querydsl. PEntity looks like a class up to 2.0.

Here is an example of a universal DAO superclass for using Querydsl JPA 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 a Q-type to your DAO class, you can use a template like this 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 the entity class with the first letter converted to lowercase.

+8
source share