문제

Is it possible to somehow get multiple objects from a one-to-many-collection by index/key, which is marked with extra lazy load? I have a big collection where I can't fetch all entries but still want to get multiple objects from it.

For example:

class System
{
  ...

  @OneToMany(mappedBy = "system")
  @MapKey(name = "username")
  @LazyCollection(LazyCollectionOption.EXTRA)
  private Map<String, User> users = new HashMap<>();

  public List<User> getUsers(List<String> usernames)
  {
    //what to do
  }

}

It's just a simple example but it portraits my problem.

I know I could just use the Criteria API or (named) queries but I try to keep the logic where it belongs to.

도움이 되었습니까?

해결책

Unfortunately it seems that Hibernate does not support loading multiple entries from a collection inside a entity.

Only ways I found:

  • use eager/lazy loading and get all objects (which won't work if there are many)
  • use extra lazy loading and get multiple objects by retrieving one by one (can hurt performance)
  • use Session.createFilter which can not be called inside an entity
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top