I’ve been fighting for the error not could initialize - no proxy for a long time, everything works fine with Eager.Fetch Therefore, such a question crept in.

Upd. I call in the controller to the method of this service.

@Service public class UserServiceImpl implements UserService { @Autowired private UserRepository repository; @Override public List<User> getAll() { return repository.findAll(); } } 

Here is my entity:

 @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "login") private String login; @ManyToOne(fetch = FetchType.Lazy, cascade=CascadeType.MERGE) @JoinColumn(name = "id_city") private City city; ... } 

It gives an error that can not initialize the city - no Proxy.

  • Can. Use nasdorove - Sergey
  • @Sergey, I added the code, look please, can you tell me something. I've been fighting for a week now ( - Tom Wally
  • I would never even think for a relation like your city to prescribe any fetch: neither lazy nor eager. And in general, he is surprisingly everywhere where lazy by default is necessary - Sergey
  • In fact, some people claim that this can be done, but the jpa provider is not obliged to actually support lazy in this case. But also errors like should not show. Perhaps this is a glitch - Sergey
  • When Lazy loads, entityManager returns a proxy. If entityManager is closed, then after accessing the field that was labeled with Lazy, you will get an error. For example, here: Guide guide = entityManager.find (Guide.class, 1L); entityManager.getTransaction (). commit (); entityManager.close (); Set <Student> students = guide.getStudents (); Perhaps in this case? - Kirill Ch

0