java - Spring Data JPA lazy fetching with collections -


i have user entity set cars (onetomany). have method:

@transactional(readonly = true) public optional<user> getuserwithcars(long id) {     return userrepository.findonebyid(id).map(u -> {         u.getcars().size();         return u;     }); } 

lazy fetching works fine. if have fetch set of users cars? tried use users.foreach(u -> u.getcars().size()); received popular no session exception.

p.s. need lazy fetch, not eager.

in spring data jpa can write query method

optional<user> findfirstwithcarsbyid(long id); 

for more information documentation


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -