Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring jpa nested projection generating incorrect query

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 !

like image 397
belnxkkk Avatar asked Nov 07 '17 13:11

belnxkkk


1 Answers

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.

like image 195
Jens Schauder Avatar answered Nov 15 '22 22:11

Jens Schauder