我已经为按特定列分组的存储库方法编写了本机查询。
我不能分组,id
因为这会破坏分组。
@Query(nativeQuery = true, value = "SELECT description, model_year, take_rate, number " +
"FROM foo f " +
"INNER JOIN bar b " +
"ON b.cycle_plan_code = b.cycle_plan_code " +
"WHERE b.programme = :programme " +
"AND b.build_event = :buildEvent " +
"AND f.vehicle_line = :vehicleLine " +
"GROUP BY description, take_rate, model_year, number")
List<FooEntity> findAllFooByBar(@Param("vehicleLine") String vehicleLine, @Param("programme") String programme,
@Param("buildEvent") String buildEvent);
我的实体有一个@Id
专栏和其他一些
@Table(name = "foos")
public class FooEntity {
@Id
private Long id;
@Column(name = "cycle_plan_code")
private String cyclePlanCode;
@Column(name = "model_year")
private String year;
@Column(name = "vehicle_line", nullable = true)
private String vehicleLine;
@Column(name = "number")
private Integer number;
@Column(name = "description")
private String description;
@Column(name = "take_rate")
private double takeRate;
}
访问存储库方法时,它会引发休眠
o.h.engine.jdbc.spi.SqlExceptionHelper : Column 'id' not found.
我试图通过将其添加到 select 子句来破解它
SELECT 0 as id
这会取得进展,但是它会抱怨查询没有返回实体的其他列vehicle_line
,model_year
我必须将它们全部添加到查询中才能使其正常工作。
我尝试了nullable = true
注释中的标志,但这不允许我从 select 子句中省略列。
如何返回仅返回一些我感兴趣的列的实体?