💡 SQL
select * from Member m where m.age>18
💡 JPQL
String jpql = "select m from Member m where m.age > 18";
List<Member> result = em.createQuery(jpql, Member.class)
.getResultList();
@Entity(name="")
으로 설정 가능 em.createQuery( "select m from Member as m" );
↓
em.createQuery( "select m from Member m" );
select 절
from 절
where 절
group by 절
having 절
orderby 절
update 절 where 절
delete 절 where 절
select count(m), // 회원수
sum(m.age), // 나이 합
avg(m.age), // 평균 나이
max(m.age), // 최대 나이
min(m.age) // 최소 나이
from Member m
TypeQuery<Member> query =
em.createQuery("select m from Member m", Member.class);
Query Query =
em.createQuery("select m.username, m.age from Member m");
반환된 객체 출력하기 위해 사용
query.getResultList()
query.getSingleResult()
:
Member singleResult1 = em.createQuery("select m from Member as m where m.username = :username", Member.class)
.setParameter("username", "member1")
.getSingleResult();
em.createQuery("select m from Member as m where m.username = ?1", Member.class)
.setParameter(1, "member1");
💡 엔티티 프로젝션
List<Member> result = em.createQuery("select m from Member m", Member.class)
.getResultList();
List<Team> result2 = em.createQuery("select m.team from Member m", Team.class)
.getResultList();
List<Team> result2 = em.createQuery("select t from Member m join m.team t", Team.class)
.getResultList();
💡 임베디드 타입 프로젝션
// 직접 가져올 수 없고 o.address가 소속되어 있는 엔티티에서 가지고 와야 함
List<Address> findAddress = em.createQuery("select o.address from Order o", Address.class)
.getResultList();
💡 스칼라 타입 프로젝션
// 여러 값 조회
List resultList = em.createQuery("select m.username, m.age from Member m")
.getResultList();
// 타입을 지정하지 못하니까 object 받아오기
Object o = resultList.get(0);
// 배열로 담겨 있어야 꺼내쓰기가 쉬움
Object[] result3 = (Object[])o;
System.out.println("username = " + result3[0]);
System.out.println("age = " + result3[1]);
List<Object[]> resultList2 = em.createQuery("select m.username, m.age from Member m")
.getResultList();
Object[] result4 = resultList2.get(0);
System.out.println("username = " + result4[0]);
System.out.println("age = " + result4[1]);
List<MemberDTO> result5
= em.createQuery("select new com.codingbox.jpql.MemberDTO(m.username, m.age) from Member m")
.getResultList();
MemberDTO memberDTO = result5.get(0);
System.out.println("memberDTO = " + memberDTO.getUsername());
System.out.println("memberDTO = " + memberDTO.getAge());
- MemberDTO
@Getter @Setter
public class MemberDTO {
private String username;
private int age;
public MemberDTO(String username, int age) {
super();
this.username = username;
this.age = age;
}
}
order by
사용setFirstResult(int startPosition)
: 조회 시작 위치(0부터 시작)setMaxResults(int maxResult)
: 조회할 데이터 수 // 나이 순으로 멤버 조회
String jpql = "select m from Member m order by m.age desc";
// 10번째 부터 20개 데이터 조회
List<Member> resultList = em.createQuery(jpql, Member.class)
.setFirstResult(10)
.setMaxResults(20)
.getResultList();
// 멤버 리스트 조회
System.out.println("result.size : " + resultList.size());
for(Member member1 : resultList) {
System.out.println("member 1 = " + member1);
}
String jpql = "select m from Member m inner join m.team t";
List<Member> resultList = em.createQuery(jpql, Member.class)
.getResultList();
String jpql2 = "select m from Member m left outer join m.team t";
List<Member> resultList2 = em.createQuery(jpql2, Member.class)
.getResultList();
String jpql3 = "select m from Member m, Team t where m.username = t.name";
List<Member> resultList3 = em.createQuery(jpql3, Member.class)
.getResultList();
String jpql = "select m from Member m where m.age > (select avg(m2.age) from Member m2)";
List<Member> resultList = em.createQuery(jpql, Member.class)
.getResultList();