JPQL

XingXiΒ·2024λ…„ 1μ›” 6일
0

JPA

λͺ©λ‘ 보기
19/23
post-thumbnail

πŸ• Reference

μžλ°” ORM ν‘œμ€€ JPA ν”„λ‘œκ·Έλž˜λ° : ꡐ보문고
μžλ°” ORM ν‘œμ€€ JPA ν”„λ‘œκ·Έλž˜λ° - 기본편 : μΈν”„λŸ°

JPQL

Java Persistence Query Language

JPA μ—μ„œ μ œκ³΅ν•˜λŠ” SQL 을 μΆ”μƒν™”ν•œ 객체지ν–₯ 쿼리 μ–Έμ–΄


ν…Œμ΄λΈ” λŒ€μƒμ΄ μ•„λ‹Œ 객체 λŒ€μƒμœΌλ‘œ 쿼리λ₯Ό μž‘μ„±ν•œλ‹€.
SQL κ³Ό μœ μ‚¬ν•˜λ‹€.

  • SELECT
  • FROM
  • WHERE
  • GROUP BY
  • HAVING
  • JOIN
    등을 지원 ν•œλ‹€.

    JPA COMMMIT μ‹œμ μ— SQL Query 둜 λ³€ν™˜λ˜μ–΄ DB 에 μ μš©λœλ‹€.

μž₯점

  1. 객체 μ€‘μ‹¬μœΌλ‘œ μž‘μ„±ν•˜κΈ° λ•Œλ¬Έμ— DB 에 μ˜μ‘΄μ μ΄μ§€ μ•Šλ‹€.
  2. ANSI ν‘œμ€€ SQL λŒ€λΆ€λΆ„μ„ μ§€μ›ν•œλ‹€.
  3. 객체λ₯Ό μ‘°νšŒν•˜κΈ° λ•Œλ¬Έμ— μž¬μ‚¬μš©μ„±κ³Ό μœ μ§€λ³΄μˆ˜μ— μ’‹λ‹€.

단점

  1. μΏΌλ¦¬μž‘μ„±μ„ λ¬Έμžμ—΄λ‘œ ν•˜κΈ° λ•Œλ¬Έμ— Compile μ‹œμ μ—μ„œ 문제λ₯Ό νŒŒμ•…ν•˜κΈ° νž˜λ“€λ‹€.
  2. λ™μ μœΌλ‘œ 쿼리λ₯Ό μž‘μ„±ν•˜κΈ° νž˜λ“€λ‹€.
  3. 쑰인, κ·Έλ£Ήν™”, μ •λ ¬κ³Ό 같은 κ³ κΈ‰ κΈ°λŠ₯μ—μ„œ 자체적인 문법을 μ‚¬μš©ν•˜μ—¬ Learning Curve 쑴재

μ‚¬μš©λ°©λ²•

Member

@Entity
public class Member
{
    @Id @GeneratedValue
    private Long id;
    private String username;

    private int age;

1.νŠΉμ§•

Entity , property λŠ” λŒ€μ†Œλ¬Έμž ꡬ별을 ν•œλ‹€.
Entity 와 Table 이름은 같지 μ•Šλ‹€. ->Entity 이름 μ¨μ•Όν•œλ‹€.
별칭은 ν•„μˆ˜λ‹€.

2.TypedQuery vs Query νƒ€μž…

TypedQuery : JPQL κ²°κ³Ό νƒ€μž…μ΄ μ •ν•΄μ‘Œμ„λ•Œ,
TypedQuery : JPQL κ²°κ³Ό νƒ€μž…μ΄ 정해지지 μ•Šμ„ λ•Œ,

JPQL κ²°κ³Ό νƒ€μž…μ΄ μ •ν•΄μ‘Œμ„λ•Œ

QUERY

select m from Member m
select m.username from Member m
---
TypedQuery<Member> query1 = em.createQuery("select m from Member m", Member.class);

Return Type 이 Memeber νƒ€μž… ( Entity ) ν˜Ήμ€ λ‹¨μΌν•œ κ°’ νƒ€μž…μΈ 경우 μ‚¬μš©

JPQL κ²°κ³Ό νƒ€μž…μ΄ μ •ν•΄μ‘Œμ„λ•Œ

QUERY

select m.username, m.age from Member m
---
Query              query2 = em.createQuery("select m.username, m.age from Member m");

Return Type 이 λ‹€μŒκ³Ό 같이 λ¬Έμžμ—΄κ³Ό μ •μˆ˜ λ‘κ°œλ‘œ λ°›μ„λ•Œμ™€ 같이
μ–΄λŠ νŠΉμ • νƒ€μž…μ΄λΌκ³  μ •ν•˜κΈ° μ–΄λ €μš΄ μƒνƒœμ—μ„œ μ‚¬μš©ν•œλ‹€.

3. getResultList() vs getSingleResult()

document

쿼리 κ²°κ³Ό κ°œμˆ˜μ— λ”°λΌμ„œ μ‚¬μš©ν•˜λŠ” λ©”μ†Œλ“œκ°€ 달라진닀

1.getResultList()

  • λ°˜ν™˜ν•˜λŠ” κ²°κ³Ό κ°œμˆ˜κ°€ 볡수 μΌλ•Œ μ‚¬μš©
  • return 값이 List
  • Query 결과값이 μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄ 크기가 0 인 빈 List λ₯Ό λ°˜ν™˜
    main
        try
        {
//            Member member = new Member();
//            member.setUsername("member1");
//            member.setAge(20);
//            em.persist(member);
            TypedQuery<Member> query1 = em.createQuery("select m from Member m", Member.class);
            List<Member> memberList = query1.getResultList();
            System.out.println(memberList.size());
            for (Member member1 : memberList) {
                System.out.println("memberList.size() : "+memberList.size());
            }

result

Hibernate: 
    /* select
        m 
    from
        Member m */ 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_
memberList.size() : 0
  • 아무것도 μ‘°νšŒλ˜μ§€ μ•Šμ„ 경우 크기가 0 인 Listλ₯Ό λ°˜ν™˜ν•˜λŠ” 것을 λ³Ό 수 μžˆλ‹€.

2. getSingleResult()

  • 단일 κ²°κ³Όκ°€ λ°˜ν™˜ μ‹œ μ‚¬μš©ν•œλ‹€.
  • κ²°κ³Όκ°€ 볡수이면 μ˜ˆμ™Έ λ°œμƒ ( NonUniqueResultException )
    Member 2개 생성 ν›„ 전체 쑰회
       try
        {
            Member member = new Member();
            member.setUsername("member1");
            member.setAge(20);
            em.persist(member);
            Member member2 = new Member();
            member2.setUsername("member1");
            member2.setAge(20);
            em.persist(member2);
            em.flush();
            em.clear();
            TypedQuery<Member> query1 = em.createQuery("select m from Member m", Member.class);
            Member findMember = query1.getSingleResult();

κ²°κ³Ό

javax.persistence.NonUniqueResultException: query did not return a unique result: 2
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:128)
	at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1560)
	at org.example.main.getResultListT.main(getResultListT.java:33)
  • κ²°κ³Όκ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄ μ˜ˆμ™Έ λ°œμƒ ( NoResultException )
    Member 생성 ν•˜μ§€ μ•Šκ³  쑰회
       try
        {
            Member member = new Member();
//            member.setUsername("member1");
//            member.setAge(20);
//            em.persist(member);
//
//            Member member2 = new Member();
//            member2.setUsername("member1");
//            member2.setAge(20);
//            em.persist(member2);
            em.flush();
            em.clear();
            TypedQuery<Member> query1 = em.createQuery("select m from Member m", Member.class);
            Member findMember = query1.getSingleResult();

κ²°κ³Ό

javax.persistence.NoResultException: No entity found for query
	at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1555)
	at org.example.main.getResultListT.main(getResultListT.java:33)

κ²°κ³Όκ°€ μ‘΄μž¬ν•˜μ§€ μ•Šμ„ μˆ˜λ„ μžˆλŠ”λ° μ˜ˆμ™Έκ°€ λ°œμƒν•˜λŠ” 것은 λ¬Έμ œκ°€ μžˆλ‹€.
μ˜ˆμ™Έμ²˜λ¦¬λ₯Ό ν•΄μ£Όμ–΄μ•Ό ν•œλ‹€.

4. Parameter Binding ( 이름 vs μœ„μΉ˜ )

이름 바인딩

Main

        try
        {
            Member member = new Member();
            member.setUsername("member1");
            member.setAge(20);
            em.persist(member);

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

            TypedQuery<Member> query1 = em.createQuery("select m from Member m where m.username = :username", Member.class);
            query1.setParameter("username","member1");
            List<Member> members = query1.getResultList();

            for (Member member1 : members) {
                System.out.println("name : "+member1.getUsername());
            }

κ²°κ³Ό

Hibernate: 
    /* select
        m 
    from
        Member m 
    where
        m.username = :username */ 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_ 
        where
            member0_.username=?
name : member1

μœ„μΉ˜ 바인딩

Main

        try
        {
            Member member = new Member();
            member.setUsername("member1");
            member.setAge(20);
            em.persist(member);

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

            TypedQuery<Member> query1 = em.createQuery("select m from Member m where m.username = ?1", Member.class);
            query1.setParameter(1,"member1");
            List<Member> members = query1.getResultList();

            for (Member member1 : members) {
                System.out.println("name : "+member1.getUsername());
            }

κ²°κ³Ό

Hibernate: 
    /* select
        m 
    from
        Member m 
    where
        m.username = :username */ 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_ 
        where
            member0_.username=?
name : member1

Parameter 바인딩을 톡해 JPQL μ—­μ‹œ 동적인 쿼리λ₯Ό μž‘μ„±ν•  수 μžˆλ‹€.
μ—¬κΈ°μ„œ 이름 으둜 바인딩 ν•˜λŠ” 방법 μœ„μΉ˜λ‘œ 바인딩 ν•˜λŠ” 방법 2κ°€μ§€λ‘œ λ‚˜λ‰œλ‹€.
μ΄λ¦„μœΌλ‘œ 바인딩 ν•  경우 Parameter 처럼 μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€.
μœ„μΉ˜λ‘œ 바인딩 ν•  경우 μ—­μ‹œ Parameter 처럼 μ‚¬μš©μ΄ κ°€λŠ₯ν•˜μ§€λ§Œ, μœ„μΉ˜ λ³€κ²½ μ‹œ μœ μ§€λ³΄μˆ˜μ— 어렀움이 있기 λ•Œλ¬Έμ— 가급적이면 이름 으둜 바인딩 ν•˜λŠ” 것이 μ’‹λ‹€.

ν”„λ‘œμ μ…˜

SELECT μ ˆμ— μ‘°νšŒν•  λŒ€μƒμ„ μ§€μ •ν•˜λŠ” 것을 λ§ν•œλ‹€.

Member

@Entity
public class Member
{
    @Id @GeneratedValue
    private Long id;
    private String username;

    private int age;

    @ManyToOne(fetch = FetchType.LAZY )
    @JoinColumn(name = "TEAM_ID")
    private Team team;

    @Embedded
    private Address address;

Team

@Entity
public class Team
{
    @Id @GeneratedValue
    private Long id;

    private String name;

    @OneToMany(mappedBy = "team")
    private List<Member> members = new ArrayList<>();

Address

@Embeddable
public class Address
{
    public Address(){}
    public Address(String city, String street, String zipcode)
    {
        this.city = city;
        this.street = street;
        this.zipcode = zipcode;
    }

    private String city;
    private String street;
    private String zipcode;

1. 전체 속성 쑰회

select m from Member m

        try
        {
            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            Member member = new Member();
            member.setUsername("member1");
            member.setAge(20);
            member.setTeam(team);
            member.setAddress(address);
            em.persist(member);

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

            List<Member> members = em.createQuery("select m from Member m").getResultList();

            for (Member member1 : members) {
                System.out.println("name : "+member1.getUsername());
                member1.setAge(12);
            }

κ²°κ³Ό

Hibernate: 
    /* select
        m 
    from
        Member m */ select
            member0_.id as id1_0_,
            member0_.city as city2_0_,
            member0_.street as street3_0_,
            member0_.zipcode as zipcode4_0_,
            member0_.age as age5_0_,
            member0_.TEAM_ID as TEAM_ID7_0_,
            member0_.username as username6_0_ 
        from
            Member member0_
Hibernate: 
    select
        team0_.id as id1_3_0_,
        team0_.name as name2_3_0_ 
    from
        Team team0_ 
    where
        team0_.id=?
name : member1
Hibernate: 
    /* update
        org.example.entity.Member */ update
            Member 
        set
            city=?,
            street=?,
            zipcode=?,
            age=?,
            TEAM_ID=?,
            username=? 
        where
            id=?

Member μ•ˆμ˜ λͺ¨λ“  ν•„λ“œλ“€μ„ 쑰회 ν–ˆμœΌλ©°, 쑰회 결과의 Member 듀은 λͺ¨λ‘ μ˜μ†μ„± Context μ—μ„œ 관리 λœλ‹€.
( member1.setAge(12); ν–ˆμ„ λ•Œ Update Query λ°œμƒ )

2. 연관관계 속성 쑰회

select m.team from Member m

            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            Member member = new Member();
            member.setUsername("member1");
            member.setAge(20);
            member.setTeam(team);
            member.setAddress(address);
            em.persist(member);

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

            List<Team> teams = em.createQuery("select m.team from Member m", Team.class).getResultList();

            for (Team team1 : teams) {
                System.out.println("name : "+team1.getName());
            }

κ²°κ³Ό

Hibernate: 
    /* select
        m.team 
    from
        Member m */ select
            team1_.id as id1_3_,
            team1_.name as name2_3_ 
        from
            Member member0_ 
        inner join
            Team team1_ 
                on member0_.TEAM_ID=team1_.id
name : A

Member 와 연관관계인 Team 을 쑰회 ν–ˆλ‹€. 객체 νƒ€μž…μ„ Team 으둜 두어야 ν•˜λ©°
Query λ°œμƒ μ‹œ JOIN 이 μžλ™μœΌλ‘œ λ°œμƒν•œλ‹€. μ‚¬μš©μž μž…μž₯μ—μ„œ JOIN 쿼리 κ°€λ‚˜κ°€λŠ” 것을 인지 μ‹œν‚€κΈ° μœ„ν•΄μ„œ 연관관계 쑰회 μ‹œμ—” λͺ…μ‹œμ μœΌλ‘œ JOIN 쿼리λ₯Ό μž‘μ„±ν•œλ‹€.

select m.team from Member m Join m.team t

3. Embedded Type 쑰회

select m.address from Member m

        try
        {
            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            Member member = new Member();
            member.setUsername("member1");
            member.setAge(20);
            member.setTeam(team);
            member.setAddress(address);
            em.persist(member);

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

                List<Address> addresses = em.createQuery("select m.address from Member m", Address.class).getResultList();

            for (Address adr : addresses) {
                System.out.println("name : "+adr.getCity());
            }

κ²°κ³Ό

Hibernate: 
    /* select
        m.address 
    from
        Member m */ select
            member0_.city as col_0_0_,
            member0_.street as col_0_1_,
            member0_.zipcode as col_0_2_ 
        from
            Member member0_
name : 1

Member 의 Embedded νƒ€μž…μ„ 쑰회

4. Scala νƒ€μž… 쑰회

select m.username, m.age from Member m

        try
        {
            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            Member member = new Member();
            member.setUsername("member1");
            member.setAge(20);
            member.setTeam(team);
            member.setAddress(address);
            em.persist(member);

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

            em.createQuery("select m.username, m.age from Member m").getResultList();

κ²°κ³Ό

Hibernate: 
    /* select
        m.username,
        m.age 
    from
        Member m */ select
            member0_.username as col_0_0_,
            member0_.age as col_1_0_ 
        from
            Member member0_

Member λ‹€λ₯Έ νƒ€μž…λ“€μ„ 쑰회, 즉 κ²°κ³Όκ°€ Query νƒ€μž… 이닀.

  • Object λ₯Ό λ°˜ν™˜
            List scala = em.createQuery("select m.username, m.age from Member m").getResultList();
            Object ob = scala.get(0);
            Object[] obArr = (Object[])ob;
            System.out.println("result = "+obArr[0]);
            System.out.println("result = "+obArr[1]);

κ²°κ³Ό

Hibernate: 
    /* select
        m.username,
        m.age 
    from
        Member m */ select
            member0_.username as col_0_0_,
            member0_.age as col_1_0_ 
        from
            Member member0_
result = member1
result = 20
  • DTO λ₯Ό μ„€μ •ν•΄μ„œ 값을 받을 수 μžˆλ‹€.
    DTO μ •μ˜
public class NameAgeDto
{
    private  String username;
    private  int    age;
    public NameAgeDto(){}
    public NameAgeDto(String username, int age)
    {
        this.username = username;
        this.age      = age;
    }

main

            List<NameAgeDto> scala = em.createQuery("select new org.example.dto.NameAgeDto(m.username, m.age) from Member m",NameAgeDto.class).getResultList();
            NameAgeDto nameAgeDto = scala.get(0);
            System.out.println("nameAgeDto.getUsername() = "+nameAgeDto.getUsername());
            System.out.println("nameAgeDto.getAge() = "+nameAgeDto.getAge());

κ²°κ³Ό

Hibernate: 
    /* select
        new org.example.dto.NameAgeDto(m.username,
        m.age) 
    from
        Member m */ select
            member0_.username as col_0_0_,
            member0_.age as col_1_0_ 
        from
            Member member0_
nameAgeDto.getUsername() = member1
nameAgeDto.getAge() = 20

select new org.example.dto.NameAgeDto(m.username, m.age) from Member m

  • μƒˆλ‘œμš΄ 데이터 νƒ€μž…μ„ μ •μ˜ν•˜κ³ 
  • μˆœμ„œμ™€ νƒ€μž…μ΄ μΌμΉ˜ν•˜λŠ” 데이터 νƒ€μž… μƒμ„±μžλ₯Ό μ •μ˜
  • 데이터 νƒ€μž…μ˜ 경둜λ₯Ό ν¬ν•¨ν•œ μƒμ„±μžλ₯Ό κΈ°μž¬ν•˜λ©΄
    λ‹€μŒκ³Ό 같이 데이터 νƒ€μž…μœΌλ‘œ 데이터λ₯Ό 얻을 수 μžˆλŠ” 것을 λ³Ό 수 μžˆλ‹€.

νŽ˜μ΄μ§• ( setFirstResult , setMaxResult )

  • 생각보닀 직관적인 API λ₯Ό μ‚¬μš©

    setFirstResult : λͺ‡λ²ˆμ§Έ λΆ€ν„° κ°€μ Έμ˜¬ 것인지
    setMaxResult : λͺ‡λ²ˆμ§Έ κΉŒμ§€ κ°€μ Έμ˜¬ 것인지

        try
        {
            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            for(int i = 0; i< 20; i++)
            {
                Member member = new Member();
                member.setUsername("member"+i);
                member.setAge(20+i);
                member.setTeam(team);
                member.setAddress(address);
                em.persist(member);
            }

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

            List<Member> members =  em.createQuery("select m from Member m order by m.age desc", Member.class)
                                                .setFirstResult(0)
                                                .setMaxResults(10)
                                                .getResultList();

            System.out.println("members.size() : "+members.size());
            for (Member member : members) {
                System.out.println("member.getAge() : "+member.getAge());
            }

κ²°κ³Ό

Hibernate: 
    select
        team0_.id as id1_3_0_,
        team0_.name as name2_3_0_ 
    from
        Team team0_ 
    where
        team0_.id=?
members.size() : 10
member.getAge() : 39
member.getAge() : 38
member.getAge() : 37
member.getAge() : 36
member.getAge() : 35
member.getAge() : 34
member.getAge() : 33
member.getAge() : 32
member.getAge() : 31
member.getAge() : 30

Member의 크기가 10 이고, age κ°€ 큰 Member λΆ€ν„°
λ‚΄λ¦Ό 차순으둜 10 개의 Member 의 λ‚˜μ΄λ₯Ό 좜λ ₯ ν•˜λŠ” 것을 λ³Ό 수 μžˆλ‹€.

  • JPA λŠ” DB DIALECT에 따라 PAGINING 처리λ₯Ό λ‹€λ₯΄κ²Œ ν•  수 μžˆλ‹€.

JOIN

1. Inner Join

select m from Member m inner join m.team t

        try
        {
            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            Member member = new Member();
            member.setUsername("member");
            member.setAge(20);
            member.setTeam(team);
            member.setAddress(address);
            em.persist(member);

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

            String query = "select m from Member m inner join m.team t";
            List<Member> members = em.createQuery(query).getResultList();

            for (Member member1 : members) {
                System.out.println(member1.toString());
            }

κ²°κ³Ό

Hibernate: 
    /* select
        m 
    from
        Member m 
    inner join
        m.team t */ select
            member0_.id as id1_0_,
            member0_.city as city2_0_,
            member0_.street as street3_0_,
            member0_.zipcode as zipcode4_0_,
            member0_.age as age5_0_,
            member0_.TEAM_ID as TEAM_ID7_0_,
            member0_.username as username6_0_ 
        from
            Member member0_ 
        inner join
            Team team1_ 
                on member0_.TEAM_ID=team1_.id
Member{id=2, username='member', age=20, address=org.example.entity.Address@45e9b12d}

2. Outer Join

select m from Member m left outer join m.team t

        try
        {
            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            Member member = new Member();
            member.setUsername("member");
            member.setAge(20);
            member.setTeam(team);
            member.setAddress(address);
            em.persist(member);

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

            String query = "select m from Member m left outer join m.team t";
            List<Member> members = em.createQuery(query).getResultList();

            for (Member member1 : members) {
                System.out.println(member1.toString());
            }

κ²°κ³Ό

Hibernate: 
    /* select
        m 
    from
        Member m 
    left outer join
        m.team t */ select
            member0_.id as id1_0_,
            member0_.city as city2_0_,
            member0_.street as street3_0_,
            member0_.zipcode as zipcode4_0_,
            member0_.age as age5_0_,
            member0_.TEAM_ID as TEAM_ID7_0_,
            member0_.username as username6_0_ 
        from
            Member member0_ 
        left outer join
            Team team1_ 
                on member0_.TEAM_ID=team1_.id
Member{id=2, username='member', age=20, address=org.example.entity.Address@45e9b12d}

3. Setter Join

select m from Member m, Team t where m.username = t.name

        try
        {
            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            Member member = new Member();
            member.setUsername("member");
            member.setAge(20);
            member.setTeam(team);
            member.setAddress(address);
            em.persist(member);

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

            String query = "select m from Member m, Team t where m.username = t.name";
            List<Member> members = em.createQuery(query).getResultList();

            for (Member member1 : members) {
                System.out.println(member1.toString());
            }

κ²°κ³Ό

Hibernate: 
    /* select
        m 
    from
        Member m,
        Team t 
    where
        m.username = t.name */ select
            member0_.id as id1_0_,
            member0_.city as city2_0_,
            member0_.street as street3_0_,
            member0_.zipcode as zipcode4_0_,
            member0_.age as age5_0_,
            member0_.TEAM_ID as TEAM_ID7_0_,
            member0_.username as username6_0_ 
        from
            Member member0_ cross 
        join
            Team team1_ 
        where
            member0_.username=team1_.name

JOIN-ON

Join 을 필터링 ν•  수 μžˆλŠ” ON 쑰건 μ ˆμ„ μ‚¬μš©ν•  수 μžˆλ‹€.
select m from Member m left outer join m.team t on t.name = 'A'

        try
        {
            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            Member member = new Member();
            member.setUsername("member");
            member.setAge(20);
            member.setTeam(team);
            member.setAddress(address);
            em.persist(member);

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

            String query = "select m from Member m left outer join m.team t on t.name = 'A'";
            List<Member> members = em.createQuery(query).getResultList();

            for (Member member1 : members) {
                System.out.println(member1.toString());
            }

κ²°κ³Ό

Hibernate: 
    /* select
        m 
    from
        Member m 
    left outer join
        m.team t 
            on t.name = 'A' */ select
                member0_.id as id1_0_,
                member0_.city as city2_0_,
                member0_.street as street3_0_,
                member0_.zipcode as zipcode4_0_,
                member0_.age as age5_0_,
                member0_.TEAM_ID as TEAM_ID7_0_,
                member0_.username as username6_0_ 
        from
            Member member0_ 
        left outer join
            Team team1_ 
                on member0_.TEAM_ID=team1_.id 
                and (
                    team1_.name='A'
                )
Member{id=2, username='member', age=20, address=org.example.entity.Address@3bc4ef12}

연관관계 μ—†λŠ” JOIN

select m from Member m left join Team t on m.username = t.name

        try
        {
            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            Member member = new Member();
            member.setUsername("member");
            member.setAge(20);
            member.setTeam(team);
            member.setAddress(address);
            em.persist(member);

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

            String query = "select m from Member m left join Team t on m.username = t.name";
            List<Member> members = em.createQuery(query).getResultList();

            for (Member member1 : members) {
                System.out.println(member1.toString());
            }

κ²°κ³Ό

Hibernate: 
    /* select
        m 
    from
        Member m 
    left join
        Team t 
            on m.username = t.name */ select
                member0_.id as id1_0_,
                member0_.city as city2_0_,
                member0_.street as street3_0_,
                member0_.zipcode as zipcode4_0_,
                member0_.age as age5_0_,
                member0_.TEAM_ID as TEAM_ID7_0_,
                member0_.username as username6_0_ 
        from
            Member member0_ 
        left outer join
            Team team1_ 
                on (
                    member0_.username=team1_.name
                )
Member{id=2, username='member', age=20, address=org.example.entity.Address@37ad042b}

SUB QUERY

일반적인 SQL μ—μ„œ μ§€μ›ν•˜λŠ” SUB QUERY λ₯Ό μ§€μ›ν•œλ‹€.
μ—¬λŸ¬κ°€μ§€ μ§€μ›ν•¨μˆ˜μ™€ 같이 μ‚¬μš© ν•  수 μžˆλ‹€.

JPQL νƒ€μž… ν‘œν˜„

  • 문자 : 'a'
  • 숫자 : 1L, 1D, 1F
  • 뢈린 : TRUE, FALSE
  • ENUM : νŒ¨ν‚€μ§€λͺ… 포함

ENUM TYPE

select m.username, m.memberType from Member m where m.memberType = :userType

public enum MemberType {
    ADMIN,USER;
}

Member

@Entity
public class Member
{
    @Id @GeneratedValue
    private Long id;
    private String username;

    private int age;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "TEAM_ID")
    private Team team;

    @Embedded
    private Address address;
    
    @Enumerated(EnumType.STRING)
    private MemberType memberType;

main

        try
        {
            Address address = new Address("1","1","1");

            Team team = new Team();
            team.setName("A");
            em.persist(team);

            Member member = new Member();
            member.setUsername("member");
            member.setAge(20);
            member.setTeam(team);
            member.setAddress(address);
            member.setMemberType(MemberType.ADMIN);
            em.persist(member);

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

            String query = "select m.username, m.memberType from Member m where m.memberType = :userType";
            List<Object[]> result = em.createQuery(query)
                    .setParameter("userType",MemberType.ADMIN)
                    .getResultList();

            System.out.println(result.get(0)[0]);
            System.out.println(result.get(0)[1]);

κ²°κ³Ό

Hibernate: 
    /* select
        m.username,
        m.memberType 
    from
        Member m 
    where
        m.memberType = :userType */ select
            member0_.username as col_0_0_,
            member0_.memberType as col_1_0_ 
        from
            Member member0_ 
        where
            member0_.memberType=?

Parameter Binding κ³Ό ν•¨κ»˜ μ‚¬μš©ν•˜μ˜€λ‹€.

0개의 λŒ“κΈ€