JPA+QueryDSL Example
in developCo-De with 0 comment

JPA+QueryDSL Example

in developCo-De with 0 comment

2022-10-31T07:57:37.png

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

@Entity
@Data
@Slf4j
public class User {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer userId;

  private String userName;

  private String phoneNo;

  private String email;
}
/**
 * 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

@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

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;
}
Comments are closed.