Custom Project interface for selecting only required fields of main table and reference entities.
public interface SimpleProjection{
Long getId();
interface Location{
Long getId();
}
interface Address{
String getCity();
}
}
Entities
@Entity
public class Simple{
@Id
private Long id;
@OneToOne
Location mainLocation;
@OneToOne
Location tempLocation;
}
@Entity
public class Location{
@Id
private Long id;
private String name;
//many more
}
Spring Repository
public interface SimpleRepository extends JpaRepository<Simple, Long> {
Optional<SimpleProjection> getById(Long id);
}
While I invoking getById() method on repository, it fetches all the column of related entities instead of defined one.
Please feel free to suggest better approach for projection as I have many related entites when I used simpler approach without projection around 200 columns get fetched that's why I am using Projection approach.