본문 바로가기

JPA

2월 17일 JPA JPQL 다형성 쿼리

JPQL 다형성 쿼리

{item} //item 을 상속받는 클래스들의 공통된 속성

-name

-price

 

{album}

-artist

 

{movie}

-director

-actor

 

{book}

-author

-isbn

 

위 같은 경우 JPA가 특별한 기능을 제공한다.

1.조회대상을 트정 자식으로 한정할수 있다(type 한정)

예)item 중에 book , movie를 조회한다

(JPQL)

select from item i

where type (i) IN (book , movie)

//where type (i) IN (book , movie) 문법으로 Item 타입들중 book과 movie만을 조회

 

(SQL)

select i from item i

where i.dtype in ('b','m')

 

2.Treat(JPA 2.1)

-자바의 타입캐스팅과 유사

-상속구조에서 부모타입을 특정 자식타입으로 다룰 때 사용

-from , where , select (하이버네이트 지원) 사용

예)부모인 Item과 자식 book이 있다.

(JPQL)

select i from item i

where treat(i as book).author = 'kim'

//where treat(i as book).author = 'kim' 문법으로 i중 book만 찾아서 특정 필드값만 가져온다.

 

(SQL)

select i.* from item i

where i.dtype = 'b' and i.author ='kim'