최대 1 분 소요

JPA Query Language (JPQL)

  • Hibernate/JPA CRUD (6)
  • Query language for retrieving onjects
  • Similar in concept to SQL
    • where, like, order by, join, in, etc…
  • However, JPQL is based on entity name and entity fields

Retrieving all Students

Retrieving Students: lastName= “Doe”

Retrieving Students using OR predicate:

Retrieving Students using LIKE predicate:

JPQL - Named Parameters

  • theLastName으로 들어오는 값을 “theData”에 넣어서 검색한다고 이해하면 됨

Development Process

  1. Add new method to DAO interface
  2. Add new method to DAO implementation
  3. Update main app

Step 1: Add new method to DAO interface

public interface StudentDAO {
	...
	List<Student> findAll();
}

Step 2: Add new method to DAO implementation

Step 3: Update main app


@SpringBootApplication  
public class CruddemoApplication {  
  
   public static void main(String[] args) {  
      SpringApplication.run(CruddemoApplication.class, args);  
   }  
  
   @Bean  
   public CommandLineRunner commandLineRunner(StudentDAO studentDAO){  
      return runner-> {  
      // createStudent(studentDAO);  
      // readStudent(studentDAO);         
      queryForStudents(studentDAO);  
      };  
   }  
  
   private void queryForStudents(StudentDAO studentDAO) {  
  
      // get a list of students  
      List<Student> theStudents = studentDAO.findAll();  
  
      // display list of students  
      for (Student tempStudent : theStudents) {  
         System.out.println(tempStudent);  
      }  
   }  
....

order by

@Override  
public List<Student> findAll() {  
    // create query  
    TypedQuery<Student> theQuery = entityManager.createQuery(
    "FROM Student order by lastName desc", Student.class);
    //  
  
    // return query results  
    return theQuery.getResultList();  
}

댓글남기기