I want to populate my object hierarchy eagerly. The problem is that at some point Hibernate stops from fetching; it leaves relation fields (lists) unpopulated.
The hierarchy is (some code is ommited like id's, getters, this part seems to be fine, hopefully):
public class User {
private Container container;
@OneToOne(fetch = FetchType.EAGER)
public Container getContainer() {
return container;
}
}
public class Container {
private List<Backpack> backpacks;
private List<Item> items;
@OneToMany(fetch = FetchType.EAGER, mappedBy="container")
@Fetch(FetchMode.SELECT)
private List<Item> getItems() {
return items;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy="container")
@Fetch(FetchMode.SELECT)
private List<Backpack> getBackpacks() {
return backpacks;
}
}
public class Backpack {
...
private Container container; /* Unidirectional relationship - ID of container which contains this backpack */
private Container backpackContainer; /* Unidirectional relationship - ID of container which represents backpack's storge */
...
@NotNull
@ManyToOne(fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
public Container getContainer() {
return container;
}
@OneToOne(fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
public Container getBackpackContainer() {
return backpackContainer;
}
}
And the result:
User(
uid=2,
name=admin,
....,
container=Container(
id=1,
items=[
Item(...),
Item(...),
Item(...),
Item(...)
],
backpacks=[
Backpack(
container=1,
backpackContainer=Container(
id=3,
items=null, /* Problem! Shouldn't be empty array [] at least or array with items? */
backpacks=null /* Problem! Shouldn't be empty array [] at least or array with backpacks? */
)
)
]
)
)
As I annotated the result, nested fields items and backpacks are null, which suggests a huge problem. Hibernate tends to use empty sets instead of nulls, also I don't want to add null checkers everywhere.
What can cause this problem?
Also, this hierarchy is not infinite-deep, user's container may have as many backpacks as it wants, but those backpacks may not contain another backpacks.
Containercontaining nulls a proxy (you can check that if it contains$in the name of class instance)?