spring jpa nested projection generating incorrect query

删除回忆录丶 提交于 2021-02-07 13:36:47

问题


Lets consider these entities

@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();

}

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

The generated query should be something like this

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

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 !


回答1:


If understand correctly the concern is that more attributes/columns get selected than necessary to fill the projections.

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.



来源:https://stackoverflow.com/questions/47159647/spring-jpa-nested-projection-generating-incorrect-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!