2014-06-13 3 views
0

이 쿼리를 jpa로 변환하려고합니다. 내 SQL 원시 쿼리.JPA 쿼리를 사용하지 않고

SELECT * 
FROM tripinfo ti 
inner join car c on ti.carid = c.id 
left join customerdetail cd on ti.customerid = cd.id 
left join station s on ti.fromstxid = s.id 
where ti.starttime is null or ti.stoptime is null 

내 JPA 쿼리 명령에

SELECT ti 
FROM TripInfo ti 
join ti.car 
left join CustomerDetail cd on ti.customer.customerDetail.id = cd.id 
left join Station st on ti.fromstxid = st.id 

는 JPA 2.0에 존재하지 않는 것.

TripInfo 클래스

public class tripInfo{ 
Long id 

@OneToOne 
JoinColumn(name = "customerId") //can be null 
Customer customer; 

@OneToOne 
@JoinColumn(name = "carid") 
Car car; 

@Column(name = "tostxid") //can be null 
Long toStxId; 

@Column(name = "fromstxid") //can be null 
Long fromStxId; 

@OneToOne(cascade = { CascadeType.ALL }) 
@JoinColumn(name = "fromdp") 
DP fromDP; 

} 

고객 클래스

public class Customer { 
    Long id 
    @OneToOne(cascade = { CascadeType.ALL }) 
    @JoinColumn(name = "customerDetail_customerid") 
    CustomerDetail customerDetail; 
} 

DP 클래스

public class DP { 
    @OneToOne(cascade = { CascadeType.ALL }) 
    @JoinColumn(name = "stationFrom") 
    Station stationFrom; 
} 

명령에를 우회하는 어떤 생각?

답변

2

조인에 대한 명시 적 'On'은 JPA 쿼리에서 허용되지 않고 대신 Entity에 정의 된 관계로 정의됩니다. 고객 정보를 얻으려는 예에서는 다음과 같이 할 수 있습니다.

SELECT ti 
join ti.car 
FROM TripInfo ti 
left join ti.customer.customerDetail 

그러나 Station Class에 정의 된 관계가 없습니다. 당신이 역 클래스, 다음이있는 경우,이 도움이

SELECT ti 
join ti.car 
FROM TripInfo ti 
left join ti.customer.customerDetail 
left join ti.station 

희망

@Column(name = "fromstxid") //can be null 
Long fromStxId; 

@Column(name = "fromstxid") 
@JoinColumn(name = "id") //Id in Station 
Station station; 

,해야하며, JPA 쿼리가 될 것입니다!

+0

제가 가 ti.car 시정 ti.customer.customerDetail가 CD를 가입 왼쪽 가입하기 TripInfo 된 TI의 다른 클래스 – redfox26

+1

SELECT TI 통과 기준 추가 ti.fromDP.stationFrom 세인트 가입 왼쪽 – Gayathri

관련 문제