Is it possible to lazy the load for non-lazy relationships in Hibernate?

I have a parent-child relationship with the lazy attribute set false, so when I get the parent class with the request, I also get its children.

It is usually advisable to download everything, parent-children, but in one case I do not need to do this.

Is there a way to avoid fetching children when I get the parent without changing the lazy = false relationship?

+5
source share
1 answer

No, It is Immpossible. The only thing you can do if you have only one case where an association should not be selected is to use DTO instead of your entity and use predictions to get only what you want:

String hql = "select firstName, lastName from User u where ...";
List<Object[]> rows = session.createQuery(hql).list();
List<UserDTO> users = new ArrayList<UserDTO>(rows.size());
for (Object[] row : rows) {
    users.add(new User((String) row[0], (String) row[1]));
}
return users;
+4
source

All Articles