我有3个表:组织,车辆和 VehicleStatus 。
带车辆的组织(一对多)。 (一个组织可以有很多车辆)。 使用VehicleStatus的车辆(一对多)(车辆具有我们在状态更改时存储的不同状态,VehicleStatus具有statusDate列以在状态更改完成时存储)。
我想获得statusType(列)具有BROKEN且属于位于NewYork的组织的车辆。
VehicleStatus(id,statusType,statusDate,Vehicle) 车辆(身份证,组织) 组织(身份证,城市)
这是SQL查询:
SELECT * FROM Vehicles V, VehicleStatus VS, Organization O
where (V.id = VS.Vehicle) AND (V.organisation = O.id)
and (O.city = "Bordeaux")
and (VS.statusType = "Broken")
and (VS.statusDate == (select Max(statusDate) from VehicleStatus groupBy vehicle where vehicle = V.id);
如何在JPQL中转换它?
感谢
答案 0 :(得分:0)
You can achieve this with the
In Vehicle entity you should have the relation as like
@ManytoOne
@joinColumns(name="organization_id" , insertable=false, updatable=false)
private Organization organization;
and in you should have the relationship to the VehicleStatus entity as below in Vehicle entity
@OneToMany
private List<VehicleStatus> vehicleStatuses;
,您的查询应该是这样的
List<Vehicle> Vehicles=createQuery("select new Vehicle(required column list ) from Vehicle v left join v.organization o left join v.vehicleStatuses vs where vs.status=?1 and o.organizationId=?2")
.setparameter(1,status)
.setParameter(2,organizationId)
.getResultList();