Spring Boot - JPQL 2

UnKnown12Β·2022λ…„ 12μ›” 12일
0

Spring Boot

λͺ©λ‘ 보기
10/13

πŸ“Œ ν”„λ‘œμ μ…˜

  • 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);

Join 을 μ‚¬μš©ν•΄μ„œ μ‘°νšŒν•˜κΈ° ( μΆ”μ²œ )

	Main 2 
    
   	List<Member> result = em.crateaQuery("select t from Member m join m.team t"  , Team.class).getResultList; 
    
    

Main 3

	
    // 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() ); 
       }

    JQPL μ—μ„œ join μ‚¬μš©ν•˜κΈ°

    	
       // 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();

πŸ“Œ JPQL μ„œλΈŒμΏΌλ¦¬μ˜ ν•œκ³„

  • 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();
profile
Hyobin12

0개의 λŒ“κΈ€