MYSQL查询的等效JPA查询

时间:2017-09-27 11:32:09

标签: java jpa jpql

我还在学习JPA框架。我在下面创建了我的mysql查询:

SELECT DISTINCT(tia.VoucherId) As Voucher, f.Name As Farm, CONCAT(far.FirstName, ' ', far.LastName) AS Farmer, s.Name Season, fa.Name As FarmingActivity
FROM Enrollment e INNER JOIN TemporaryInputsAllocated tia ON e.Farm_Id = tia.Farm_Id
INNER JOIN Farm f ON e.Farm_Id = f.Id
INNER JOIN Farmer far ON f.Farmer_Id = far.Id
INNER JOIN Season s ON tia.Season_Id = s.Id
INNER JOIN FarmingActivity fa on e.FarmingActivity_Id = fa.Id;

什么是等效的JPA查询?

1 个答案:

答案 0 :(得分:0)

如果您有外键,则不需要内部联接条款。您可以像这样更改查询:

SELECT DISTINCT(tia.VoucherId) As Voucher, e.Farm_Id.Name As Farm, CONCAT(e.Farm_Id.Farmer_Id.FirstName, ' ', e.Farm_Id.Farmer_Id.LastName) AS Farmer, tia.Season_Id.Name Season, e.FarmingActivity_Id.Name As FarmingActivity FROM Enrollment e, TemporaryInputsAllocated tia where e.Farm_Id = tia.Farm_Id

此查询返回一个对象列表。