select μ μ μ‘°νν λμμ μ§μ νλ κ²
select m from Member m
= Member μν°ν° μ 체 μ‘°ν
select m.team from Member m
= Member μν°ν° μμ team μ μ‘°ν
List<Member> result = em.crateaQuery("select m from Member m" , Member.class).getResultList; // result κ°μ²΄μ 0λ²μ§Έ νλͺ©μ // age = 20 μΌλ‘ μ€μ νλ€ // IF ) μ΄ λΆλΆμ΄ update κ° λλ€λ©΄ μμμ± μ»¨ν μ€νΈμμ κ΄λ¦¬κ° λλ€ λ λ» // update κ° X = μμμ± μ»¨ν μ€νΈκ° μλ Member findMember = result.get(0); findMember.setAge(20);
Main 2
List<Member> result = em.crateaQuery("select t from Member m join m.team t" , Team.class).getResultList;
// JPQL 쿼리 μ‘°ν - Order ν
μ΄λΈ , address λ°μ΄ν°
1. em.createQuery("select o.address from Order o", Address.class ).getResultList();
2. em.createQuery("select o.address from Address o", Address.class ).getResultList();
1λ²μ Order ν μ΄λΈμ addressκ° μ‘°νκ° λμ§λ§
2λ²μ Address ν
μ΄λΈμ΄ μν°ν° κ°μ²΄κ° μλμΌλ‘
μ‘°νκ° λΆκ°λ₯
em.createQuery("select distinct m.username, m.age from Member m").getResultList();
// λλ²μ§Έ νλΌλ―Έν° κ° μλ€ = λ°ν νμ
μ΄ μλ€
// λ°ν νμ
μ΄ μκΈ°μ λ¨μ κ°μ²΄λ₯Ό μμ±νμ¬
// ν΄λΉ κ°μ²΄μ κ°μ λ΄λ λ°©λ²
Object o = result.get(0);
Object[] result = (object[])o;
syso("username= " + result[0] );
syso("age = " + result[0] );
ν΄λΉ λ°μ΄ν°κ° object λ°°μ΄μμ μμλλ‘ ν λΉλλ€
κ°μ΄ λ§μμ§λ©΄ μ΄μΊ?
List<Object[]> resultList = em.createQuery("select m.username, m.age from Member m").getResultList;
object[] result = resultList.get(0);
syso("username= " + result[0] );
syso("age = " + result[0] );
List<Object[]> resultList = em.createQuery("select new MemberDTO ν¨ν€μ§ μ£Όμ (m.username, m.age) from Member m").getResultList;
MemberDTO mDTO = new MemberDTO;
mDTO.getUsername();
mDTO.getAge();
ν΅ν΄ μ‘°νκ° κ°λ₯νλ€
MemberDTO ν¨ν€μ§ μ£Όμλ₯Ό
κ°μ²΄λ₯Ό μμ±νλ―μ΄ new λ₯Ό λΆμ¬ JPQL μ£Όμμ μ μΈ ν
() μμ μ‘°νν λ°μ΄ν°λ₯Ό μ μΈνλ€
**μ μΈκ³Ό λμμ MemberDTO λ³μμ ν λΉλλ€. **
setFirstResult(int startPoint)
= μ‘°ν μμ μμΉ ( λν΄νΈ κ° 1 )
setMaxResults(int maxResult)
= μ‘°νν λ°μ΄ν° μ μ€μ
= λͺλ²μ§Έ λΆν° λͺ κ° κΉμ§ μ‘°ν ?
11~20 μ μ‘°ννκ³ μ νλ©΄
setFirstResult(10)
setMamxResult(20)
μΌλ‘ μ€μ νλ©΄ λλ€ .
String jpql = "select m from Member m order by m.id";
List<Member> resultList = em.createQuery( jpql , Member.class )
.setFirstResult(10)
.setMaxResult(20)
.getResultList();
jpql λ³μμ μ£Όμμμ
11λ²μ§Έ λ°μ΄ν° λΆν°
20λ²μ§Έ λ°μ΄ν°κΉμ§
μ 체 μ‘°ν ( Member νμ
μΌλ‘ λ¦¬ν΄ )
// size() = 20
resultList.size()
// ToString ν΅ν΄ μ 체쑰ν
// 11λ² ~ 30λ² member μ μ λ³΄κ° μ‘°νλλ€
for ( Member m1 : resultList ) {
syso(" member1 = " + member1.toString() );
}
// inner join = innerλ μλ΅κ°λ₯ νλ€
String sql = "select m from member m inner join m.team t where t.name = :teamName";
List<member> resultList = em.createQuery(sql, Member.class)
.getResultList();
String sql = "select m from member m left outer join m.team t";
List<member> resultList = em.createQuery(sql, Member.class)
.getResultList();
Where, having μ μμλ§ μ¬μ©κ°λ₯ν κΈ°λ² = JPA νμ€ μ€ν
select μ μμλ κ°λ₯νλ€ ( Hibernate μ§μ )
from μ μ μλΈμΏΌλ¦¬λ νμ¬ JPQL μμ λΆκ°λ₯
String sql = "select m from Member m where m.age > (select avg(m2.age) from Member m2 "
List<member> resultList = em.createQuery(sql, Member.class)
.getResultList();