spring jpa 嵌套投影生成不正确的查询 [英] spring jpa nested projection generating incorrect query
问题描述
让我们考虑这些实体
@Entity
public class Room{
@Id
private Integer id;
private String number;
private String floor;
@ManyToOne
private RoomType roomType;
// Setters & Getters
}
@Entity
public class RoomType{
@Id
private Integer id;
private String name;
private String description;
private Boolean enabled;
// Setters & Getters
}
还有这个用于与存储库类一起投影的接口
And also this interface for projection alongside repository class
public interface RoomList{
public Number getId();
public String getNumber();
public RoomType getRoomType();
interface RoomType {
String getName();
}
}
@Repository
public interface RoomRepository extends JpaRepository<Room,Integer>{
public Collection<RoomList> findAllProjectedBy();
}
现在,如果我查看生成的 SQL
Now if I look at generated SQL
select
room0_.id as col_0_0_,
room0_.number as col_1_0_,
roomtype1_.id as id1_3_,
roomtype1_.description as descript2_3_,
roomtype1_.enabled as isActive3_3_,
roomtype1_.name as name5_3_
from
Room room0_
inner join
roomType roomtype1_
on room0_.roomType_id=roomtype1_.id
生成的查询应该是这样的
select
room0_.id as col_0_0_,
room0_.number as col_1_0_,
roomtype1_.name as name5_3_
from
Room room0_
inner join
roomType roomtype1_
on room0_.roomType_id=roomtype1_.id
有人可以解释这种行为还是这是一个错误?我们还有什么其他选择可以达到这种结果.我已经尝试过 JPA entitygraph,但是在 hibernate 中还没有完全支持图形类型提取,我也不想使用构造函数 jpql 查询.谢谢!
Can someone explain this behaviour or either this is a bug ? also what other options do we have achieve this kind of result. I already tried JPA entitygraph but graph type fetch is not yet fully supported in hibernate, i don't want to use constructor jpql query either. Thanks !
推荐答案
如果理解正确,问题是会选择更多的属性/列来填充投影.
If understand correctly the concern is that more attributes/columns get selected than necessary to fill the projections.
正如我刚刚在 issue DATAJPA-1218 中所描述的,这就是投影的工作原理当前的 Spring 数据.引用实体的属性不限于投影中使用的属性.
As I just described in issue DATAJPA-1218 this is just how projections work in Spring Data currently. The attributes of referenced entities do not get limited to those used in the projection.
这篇关于spring jpa 嵌套投影生成不正确的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!