JPA Query Language (JPQL)
JPQL(Java Persistence Query Language)μ JPA(Jakarta Persistence API) μμ μ¬μ©λλ Object-Oriented query language λ‘, Database Table μ΄ μλΒ Entity Object λ₯Ό λμμΌλ‘ query λ₯Ό μμ±νλ λ° μ¬μ©λλ€.
SQL κ³Ό λ¬Έλ²μ΄ μ μ¬νμ§λ§, JPQL μ Entity object μ€μ¬μ μ΄λ©°, JPA κ° μ΄λ₯Ό SQL λ‘ λ³ννμ¬ μ€ννλ€. μ¦, JPQL μ enitity name κ³Ό entity fields λ₯Ό κΈ°λ°μΌλ‘ λκ³ μλ€. λν μ£Όλ‘ database μμ object λ₯Ό retrieve ν λ μ¬μ©λλ€.
Retrieving all Students
TypedQuery<Student> theQuery = entityManager.createQuery("FROM Student", Student.class);
List<Student> students = theQuery.getResultList();μμ κ°μ΄ μμ±νλ©΄ datatbase table μμ λͺ¨λ Student object λ€μ retrieving ν μ μλ€. μ½λλ₯Ό 보면 μκ² μ§λ§, JPQL syntax λ μ€μ DB μ table name μ΄λ column name μ΄ μλλΌ Entity Class μ name κ³Ό fields λ₯Ό κΈ°λ°μΌλ‘ μμ±νλ€.
TypedQuery λ return type μ΄ λͺ
νν λ μ¬μ©νλ©°, λͺ
ννμ§ μμ λμλ Query λ₯Ό μ¬μ©νλ€. Query λ₯Ό μ¬μ©ν λμλ λ€μκ³Ό κ°μ΄ μ¬μ©νλ€.
Query query = entityManager.createQuery(jpql);
List<Object[]> result = query.getResultList();Retrieving Students: lastName='lastname'
.createQuery("FROM Student WHERE lastName='Slot'", Student.class);μ΄λ²μλ νΉμ column μ λν κ°μ μ§μ νμ¬ μνλ object λ§ retrieving ν΄λ³΄μ.
Retrieving Students: Using OR Predicate
.createQuery("FROM Student WHERE lastName='Slot' OR fistName='Alisson'", Student.class);Retrieving Students: Using LIKE Predicate
.createQuery("FROM Student WHERE email LIKE '%liverpool.com'", Student.class);Parmeter Binding
JQPL μμλ query μ μ¬μ©λλ dynamic value λ₯Ό μ²λ¦¬νκΈ° μνμ¬, μ¦ query μ κ°μ dynamic νκ² μ λ¬νκΈ° μνμ¬ Parameter Binding μ μ¬μ©νλ€.
- Named Parameter Binding
String jpql = "SELECT s FROM Student s WHERE s.name = :name";
query.setParameter("name", "John");Named Parameter λ query μμ parameter name μ :name μ syntax λ‘ define νκ³ , μ΄λ₯Ό setParameter method λ₯Ό ν΅νμ¬ κ°κ³Ό μ°κ²°νλ λ°©μμ΄λ€.
- Positional Parameter Binding
String jpql = "SELECT s FROM Student s WHERE s.name = :name";
query.setParameter(1, "John");Positional Parameter λ query μμ parameter λ₯Ό ?number μ syntax λ‘ define νκ³ , μ΄λ₯Ό setParameter method λ₯Ό ν΅νμ¬ κ°κ³Ό μ°κ²°νλ λ°©μμ΄λ€.
Positional Parameter Binding μ query κ° λ³΅μ‘ν΄μ§μλ‘ κ΄λ¦¬κ° μ΄λ €μμ§κ³ κ°λ μ±μ΄ λ¨μ΄μ§λ€. λ°λΌμ μΌλ°μ μΌλ‘λ Named Parameter Binding μ μ¬μ©νλ κ²μ΄ Conventional νλ€.
JPQL - SELECT clause
μμμ query λ₯Ό μμ±ν λ, μ΄λ€ query λ SELECT clause λ₯Ό ν¬ν¨νκ³ μκ³ , λ€λ₯Έ query λ λͺ
μμ μΌλ‘ μ μ΄μ£Όμλ€.
Hibernate λ₯Ό μ¬μ©νλ νκ²½μμλ HQL(Hibernate Query Language) μ μ 곡νκΈ° λλ¬Έμ SELECT λ₯Ό μλ΅ν΄λ λλ€. νμ§λ§ strict ν JPQL μμλ λ°λμ SELECT clause λ₯Ό μ¬μ©ν΄μΌ νλ€.
κ·Έλ¬λ Hibernate λ₯Ό μ¬μ©νλ κ²½μ° SELECT λ₯Ό μλ΅ν΄λ λμνμ§λ§, λͺ
μμ μΌλ‘ μ¬μ©νλ κ²μ΄ μ½λμ κ°λ
μ±κ³Ό μ μ§λ³΄μμ±μ λμ΄λ λ° λμμ΄ λλ€.
JPQL - Development
Step 1: Add new method to DAO Interface
public interface StudentDAO {
...
List<Student> findAll();
}Step 2: Define DAO Implementation
public class StudentDAOImpl implements StudentDAO {
private EntityManager entityManager;
...
@Override
public List<Student> findAll() {
TypedQuery<Student> theQuery = entityManager.createQuery("SELECT s FROM Student s", Student.class)
return theQuery.getResultList();
}
}λ§μ°¬κ°μ§λ‘ λ¨μν SELECT μ ν΄λΉνλ query μ΄κΈ° λλ¬Έμ @Transactional annotation μ μ¬μ©ν νμλ μλ€.
Step 3: Update Main Application
@SpringBootApplication
public class CruddemoApplication {
public static void main(String[] args) {
SpringApplication.run(CruddemoApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(StudentDAO studentDAO) {
return runner -> {
queryForStudents(studentDAO);
}
}
private void queryForStudents(StudentDAO studentDAO) {
// get list of students
List<Student> theStudents = studentDAO.findAll();
// display list of students
for (Student tempStudent : theStudents) {
System.out.println(tempStudent);
}
}
}JPQL - Test

μμ κ°μ΄ Student table μ μ‘΄μ¬νλ λͺ¨λ object λ₯Ό retrieving ν κ²μ λ³Ό μ μλ€.