我尝试在jpa中转换此查询 我的sql本地查询。
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中似乎不存在on命令。
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;
}
是否有想要绕过on命令?
答案 0 :(得分:2)
明确' On'在JPA查询中不允许连接,而是由实体中定义的关系定义。在您获取客户详细信息的示例中,您可以执行以下操作。
SELECT ti
join ti.car
FROM TripInfo ti
left join ti.customer.customerDetail
但是,我没有看到为Station Class定义任何关系。如果你有Station Class,那么,
@Column(name = "fromstxid") //can be null
Long fromStxId;
应该是,
@Column(name = "fromstxid")
@JoinColumn(name = "id") //Id in Station
Station station;
JPA Query将是,
SELECT ti
join ti.car
FROM TripInfo ti
left join ti.customer.customerDetail
left join ti.station
希望这有帮助!