Post

2023-04-26-TIL

2023-04-26-TIL

Today I Learned

JPAQueryFactory Construct with EntityManager

  • https://ksabs.tistory.com/167
  • https://zkdlu.github.io/2021-04-13/Jpa03-QueryDSL/
  • https://www.baeldung.com/intro-to-querydsl

Q. Is it necessary to inject EntityManager to configure JPAQueryFactory?

A. Yes, it is necessary to inject the EntityManager to configure a JPAQueryFactory in JPA (Java Persistence API).

The JPAQueryFactory is a class provided by the Querydsl library, which is commonly used to build type-safe queries in JPA. It requires an EntityManager to be created, which is used to obtain a JPQLQuery instance, which in turn can be used to build and execute type-safe queries.

In addition, the EntityManager is responsible for managing the persistence context, which is used to track the state of managed entities, and to perform operations such as persist, merge, and remove.

Therefore, it is necessary to inject the EntityManager into the JPAQueryFactory in order to create and execute type-safe queries against the persistence context managed by JPA.

Q. How to configure JPAQueryFactory?

A. To configure a JPAQueryFactory in JPA, you need to follow these steps:

  1. Inject an EntityManager into your class:
1
2
@PersistenceContext
private EntityManager entityManager;
  1. Create a JPAQueryFactory instance using the EntityManager:
1
JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

Once you have created a JPAQueryFactory instance, you can use it to build and execute type-safe queries using Querydsl.

Here’s an example of using JPAQueryFactory to build and execute a query:

1
2
3
4
5
QEmployee employee = QEmployee.employee;
List<Employee> employees = queryFactory.selectFrom(employee)
    .where(employee.department.name.eq("IT"))
    .orderBy(employee.name.asc())
    .fetch();

In this example, we’re using Querydsl to build a type-safe query that selects all employees from the Employee entity where their department name is “IT”, and orders the results by their name in ascending order.

Finally, we execute the query using the fetch() method to obtain the results as a list of Employee objects.

Note that this is just a basic example, and there are many other ways to build and execute type-safe queries using JPAQueryFactory and Querydsl.

Q. Why do I need to inject EntityManager to JPAQueryFactory?

A. You need to inject EntityManager to JPAQueryFactory to create an instance of JPAQueryFactory that is associated with a specific EntityManager.

EntityManager is the main interface for interacting with the persistence context in JPA. It provides methods for persisting, querying, and deleting entities, among other things. JPAQueryFactory is a utility class that provides a fluent API for building type-safe queries in JPA using Querydsl.

When you create an instance of JPAQueryFactory using an EntityManager, the JPAQueryFactory instance is associated with the EntityManager. This means that any queries executed using the JPAQueryFactory instance will use the same persistence context as the EntityManager, ensuring consistency and avoiding issues with detached entities.

In addition, by using JPAQueryFactory with EntityManager, you can take advantage of Querydsl’s type-safe query building capabilities, which helps reduce errors and makes it easier to write and maintain complex queries.

In summary, by injecting EntityManager to JPAQueryFactory, you can create a type-safe query builder that is associated with a specific EntityManager, ensuring consistency and taking advantage of Querydsl’s type-safe query building capabilities.

QueryDSL Configuration with JPA

  • https://batory.tistory.com/496
  • https://jojoldu.tistory.com/372
  • http://querydsl.com/static/querydsl/latest/reference/html/ch02.html#jpa_integration
  • https://growing-up-constantly.tistory.com/55
  • https://github.com/querydsl/querydsl/tree/master/querydsl-jpa

JPAQuery vs JPAQueryFactory

  • https://www.inflearn.com/questions/37565/%EC%95%88%EB%85%95%ED%95%98%EC%84%B8%EC%9A%94-jpaquery-lt-t-gt-%EC%99%80-jpaqueryfactory-%EC%B0%A8%EC%9D%B4%EC%97%90-%EB%8C%80%ED%95%B4-%EB%AC%B8%EC%9D%98-%EB%93%9C%EB%A6%BD%EB%8B%88%EB%8B%A4
  • https://stackoverflow.com/questions/62569696/difference-between-jpaquery-and-jpaqueryfactory
  • http://querydsl.com/static/querydsl/4.4.0/apidocs/com/querydsl/jpa/impl/JPAQueryFactory.html
  • https://github.com/querydsl/querydsl/issues/2081

QueryDSL Tutorial

  • https://tecoble.techcourse.co.kr/post/2021-08-08-basic-querydsl/
  • http://querydsl.com/static/querydsl/5.0.0/reference/html_single/

Performance Issue of Paginaition

  • https://www.eversql.com/faster-pagination-in-mysql-why-order-by-with-limit-and-offset-is-slow/
  • https://velog.io/@minsangk/%EC%BB%A4%EC%84%9C-%EA%B8%B0%EB%B0%98-%ED%8E%98%EC%9D%B4%EC%A7%80%EB%84%A4%EC%9D%B4%EC%85%98-Cursor-based-Pagination-%EA%B5%AC%ED%98%84%ED%95%98%EA%B8%B0
  • https://engineer-mole.tistory.com/305
  • https://velog.io/@hslim8888/%ED%8E%98%EC%9D%B4%EC%A7%80%EB%84%A4%EC%9D%B4%EC%85%98-%EC%84%B1%EB%8A%A5-%EA%B0%9C%EC%84%A0%ED%95%98%EA%B8%B0
  • https://medium.com/swlh/sql-pagination-you-are-probably-doing-it-wrong-d0f2719cc166
  • https://docs.gitlab.com/ee/development/database/pagination_performance_guidelines.html
  • https://mariadb.com/kb/en/pagination-optimization/
  • https://stackoverflow.com/questions/19118532/pagination-in-sql-performance-issue

MySQL N-gram Parser

  • https://dev.mysql.com/blog-archive/innodb-full-text-n-gram-parser-ko/
  • https://gngsn.tistory.com/163
  • https://dev.mysql.com/doc/refman/5.7/en/fulltext-search-ngram.html

The Future of Reactive Programming

  • https://www.reddit.com/r/java/comments/oxcoeq/brian_goetz_i_think_project_loom_is_going_to_kill/
This post is licensed under CC BY 4.0 by the author.