Define Query Dsl Dependency
<querydsl.version>5.0.0</querydsl.version>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
Define Process Maven Plugins (QueryDSL Apt)
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
</plugin>
Maven Clean / Complile
Will Scan @Entity Component
@Entity
@Data
@Slf4j
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer userId;
private String userName;
private String phoneNo;
private String email;
}
Generate Related Q$Entity
/**
* QUser is a Querydsl query type for User
*/
@Generated("com.querydsl.codegen.DefaultEntitySerializer")
public class QUser extends EntityPathBase<User> {
private static final long serialVersionUID = -1059457371L;
public static final QUser user = new QUser("user");
public final StringPath email = createString("email");
public final StringPath phoneNo = createString("phoneNo");
public final NumberPath<Integer> userId = createNumber("userId", Integer.class);
public final StringPath userName = createString("userName");
public QUser(String variable) {
super(User.class, forVariable(variable));
}
public QUser(Path<? extends User> path) {
super(path.getType(), path.getMetadata());
}
public QUser(PathMetadata metadata) {
super(User.class, metadata);
}
}
Usage
Base on EntityManager
Declear JPAQuery<?>
Build Query Call, eg. from().where().fetch().
@Repository
@Transactional
public class UserDaoImpl implements UserDao {
@PersistenceContext EntityManager entityManager;
@Override
public User save(User user) {
entityManager.persist(user);
return user;
}
@Override
public List<User> list(User user) {
QUser qUser = QUser.user;
JPAQuery<User> jpaQuery = new JPAQuery<>(entityManager);
return jpaQuery.from(qUser).fetch();
}
}
Memos
Transaction Annotations
2022-10-31 16:18:55.680 ERROR 11617 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call] with root cause
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:295) ~[spring-orm-5.3.23.jar:5.3.23]
at com.sun.proxy.$Proxy94.persist(Unknown Source) ~[na:na]
at top.moma.guide.repos.UserDaoImpl.save(UserDaoImpl.java:19) ~[classes/:na]
at top.moma.guide.repos.UserDaoImpl$$FastClassBySpringCGLIB$$d2addf89.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.23.jar:5.3.23]
@Repository
/** @PersistenceContext need work with @Transactional*/
@Transactional
public class UserDaoImpl implements UserDao {
@PersistenceContext EntityManager entityManager;
}
Others Examples
本文由 Ivan Dong 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Jul 7, 2023 at 09:16 am