让我们考虑这些实体
@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
}
还有用于与存储库类一起投影的界面
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
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查询。谢谢!
答案 0 :(得分:4)
如果理解正确,则需要选择更多的属性/列来填充预测。
正如我在issue DATAJPA-1218中所描述的那样,这正是预测Spring数据目前的工作方式。引用实体的属性不限于投影中使用的属性。