Get lazy properties in Spring projections

I'm currently trying to evaluate the possibilities of using Spring Data JPA.

Trying to use Projections, I'm currently stuck trying to get certain Eager properties.

I have a simple object that lazily references another object with a foreign key. Now I would like to define various forecasts for the previous object. "Primitive" properties are projected into the projection interface, but trying to project another object / projection leads to the fact that it is still lazy.

Now I would like to say Spring / JPA to look forward to loading objects / projections inside forecasts. One possible way would be to use EntityGraphs (they worked well), but I would have to create repositories for each method using different graphs. The question is, what other methods exist?

Example:

Buyer of the object:

@Entity public class Buyer { private Integer id; private String someProperty; private User user; ... @OneToOne( fetch = FetchType.LAZY) @JoinColumn( name = "CAB_USR_ID", referencedColumnName = "ID", updatable = false, nullable = true, foreignKey = @ForeignKey(name = "FK_CAB_USR")) public User getUser() { return user; } public void setUser(User user) { this.user = user; } } 

User:

 @Entity public class User { private Integer id; private String name; ... } 

Buyer Predictions

 public interface BuyerCProjection { Integer getId(); UserProjection getUser(); } 

Projection User

 public interface UserProjection { Integer getId(); String getName(); } 
Repository

I would like to use

 public interface BuyerRepository extends Repository<Buyer, Integer> { <T> List<T> findBy(Class<T> t); } 
+5
source share

Source: https://habr.com/ru/post/1264290/


All Articles