First of, I am really new to QueryDSL.
I'm using a Spring + Hibernate environment.
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.
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
public class QEntity extends PEntity<Entity>{
...
public static final QEntity entity = new QEntity("entity");
...
//constructors
}
And the GenericDao would look like this:
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.
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. Also, what does the String in the constructor parameter signify?
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.
Without a reference to the QEntity class it is difficult, so make sure you provide an instance to it to your 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. Also, what does the String in the constructor parameter signify?
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. PEntity looks like a pre-2.0 class.
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
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
The variable name will be the simple name of you entity class with the first letter converted to lowercase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With