Hi everbody,
my problem is the following: I have an entity class
"Partner" which has a bidirectional
OneToMany relation to entity class "Role".
"Role" is abstract and has a concrete subclass
"Employee". The Entity class "Employee" has a unidirectional
OneToOne-Relation to entity class
"BankAccount".I have a use-case-based NamedQuery which retrieves all Partners:
Code:
@NamedQuery(name = "getAllPartners", query = "SELECT p FROM Partner as p"),
The collection attribute getter for "Roles" in entity class "Partner" is annotated like this (Focus on @BatchSize-annotation):
Code:
@OneToMany(mappedBy = "partner", cascade = { CascadeType.MERGE,
CascadeType.PERSIST }, fetch = FetchType.LAZY)
@BatchSize(size = 100)
@IndexColumn(name = "id")
@JoinColumn(name = "partner_id", referencedColumnName = "id")
public Set<Role> getRoles() {
return roles;
}
In my specific Data Access Object method I am executing the query and iterating through the 50 results like this:
Code:
Query q = em.createNamedQuery("getAllPartners").setMaxResults(50);
List<Partner> l = q.getResultList();
// BatchRead
for (Partner partner : l) {
partner.getRoles().size();
// Here is my PROBLEM
for (Role role : partner.getRoles()) {
((Employee) rolle).getBankAccount();
}
}
Without trying to also get the "BankAccount" entity ,everything works just fine and Hibernate generates a SELECT with a huge
WHERE...IN(...)-clause.
With it, it ends up in a
LazyInitializationException because no additional
SELECT for the BankAccount is placed at any time and I am accessing the BankAccount on a detached object.
So basically the problem I am experiencing, is that in my case there seems to be no opportunity to also batch read the associated entity class "BankAccount" without using a static eager. Or is there a solution for this kind of problem?
Any help is appreciated!!! Thanks a lot in advance!