[JPA] JQPL JOIN (Inner Join/Left Join/Seta Join)

반응형
728x90
반응형

1) 연관관계 적용

Member member = new Member();
member.setUsername("kimseohae");
member.setAge(10);

Team team = new Team();
team.setName("team1");
team.setName("team");

member.setTeam(team);

em.persist(team);
em.persist(member);

em.flush();
em.clear();

 

2) Join 예제

- Inner

TypedQuery<Member> query = em.createQuery("select m from Member as m inner join m.team t", Member.class);

List<Member> resultList = query.getResultList();
System.out.println("result.size = " + resultList.size());

 

(수행쿼리)
/* select
        m 
    from
        Member as m 
    inner join
        m.team t */ select
            member0_.id as id1_0_,
            member0_.age as age2_0_,
            member0_.TEAM_ID as team_id4_0_,
            member0_.username as username3_0_ 
        from
            MEMBER member0_ 
        inner join
            TEAM team1_ 
                on member0_.TEAM_ID=team1_.id

 

- Left

TypedQuery<Member> query = em.createQuery("select m from Member as m left join m.team t", Member.class);

List<Member> resultList = query.getResultList();
System.out.println("result.size = " + resultList.size());

 

(수행쿼리)
/* select
        m 
    from
        Member as m 
    left join
        m.team t */ select
            member0_.id as id1_0_,
            member0_.age as age2_0_,
            member0_.TEAM_ID as team_id4_0_,
            member0_.username as username3_0_ 
        from
            MEMBER member0_ 
        left outer join
            TEAM team1_ 
                on member0_.TEAM_ID=team1_.id

 

- Seta

TypedQuery<Member> query = em.createQuery("select m from Member m, Team t where m.username = t.name", Member.class);

List<Member> resultList = query.getResultList();
System.out.println("result.size = " + resultList.size());

 

(수행쿼리)
/* select
        m 
    from
        Member m,
        Team t 
    where
        m.username = t.name */ select
            member0_.id as id1_0_,
            member0_.age as age2_0_,
            member0_.TEAM_ID as team_id4_0_,
            member0_.username as username3_0_ 
        from
            MEMBER member0_ cross 
        join
            TEAM team1_ 
        where
            member0_.username=team1_.name

 

반응형

Designed by JB FACTORY