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 !
findAllProjectedBy? When you haven't described when the Query is generated what operation you performed.findAllProjectedBy()works exactly likefindAll()except I can only returnList<Room>infindAll()but i can returnList<RoomList>infindAllProjectedBy()or whatever i want.RoomTypeas aninterfaceand as well asclass, have you considered changing it.RoomTypeas an class but not as an interface. Interface name isRoomListand configuration is as per spring data jpa reference documentation link. editRoomTypeinner interface is required for nested projections.RoomTypeinner interface is required by spring data jpa for nested projections also all configurations are correct.