I have a number of JPA classes containing fields of a certain type in various combinations. This looks like this:
// entity to initialize
@Entity
class Type {
...
@ManyToOne(fetch = LAZY)
B b;
}
// containers
@Entity
class A {
...
@ManyToOne(fetch = LAZY)
Type type;
}
@Entity
public class B {
...
@ManyToOne
C c;
@OneToMany(fetch = LAZY)
List<Type> possibleTypes;
}
@Entity
public class C {
...
@OneToMany
List<B> b;
@OneToOne(fetch = LAZY)
Type type;
}
Is there a way to initialize all Type
proxies with one SELECT
without building a manual request or initializing fields one by one with a number of Hibernate.initialize()
calls? Something like
Set<Type> possibleProxies = new HashSet<>();
possibleProxies.add(a1.getType());
for (B b : c1.getB()) {
possibleProxies.addAll(b.getPossibleTypes());
}
possibleProxies.add(c1.getType());
initializeAll(possibleProxies);
so that Hibernate or related library would check if some of the objects are already initialized and build a correct SELECT
query according to JPA mapping.