9

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 !

9
  • Can you share the definition of findAllProjectedBy ? When you haven't described when the Query is generated what operation you performed. Commented Nov 7, 2017 at 13:56
  • findAllProjectedBy() works exactly like findAll() except I can only return List<Room> in findAll() but i can return List<RoomList> in findAllProjectedBy() or whatever i want. Commented Nov 7, 2017 at 14:00
  • You have RoomType as an interface and as well as class, have you considered changing it. Commented Nov 7, 2017 at 14:04
  • I have RoomType as an class but not as an interface. Interface name is RoomList and configuration is as per spring data jpa reference documentation link. edit RoomType inner interface is required for nested projections. Commented Nov 7, 2017 at 14:07
  • 1
    RoomType inner interface is required by spring data jpa for nested projections also all configurations are correct. Commented Nov 7, 2017 at 14:11

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.