I am having a problem with bidirectional relationships, in this pseudo example
I have ObjectA ->ObjectB
ObjectA is many-to-one to ObjectB
I use a JPQL statement to retrieve all the objects using LEFT JOIN FETCH. The problem comes in when I walk down to ObjectB I can not get back to ObjectA. When calling ObjectB.getObjectAs().get(0) I get the famous
Code:
"org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role"
My analysts of the problem is hibernate trying to load all relationships for ObjectB->ObjectA . If that's the case how can I get it to stop doing so, and just use the relationship it already has, If not what is actually causing the exception.
Here is my code if I failed to describe the problem correctly.
Code:
The Query I use
Select screensetDefinition FROM ScreensetDefinition screensetDefinition LEFT JOIN FETCH screensetDefinition.screen screen WHERE screensetDefinition.id = :id
Code:
Object A
@ManyToOne(fetch=FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name="Screen_ID", nullable=false)
public Screen getScreen()
{
return this.screen;
}
public void setScreen(Screen screen)
{
this.screen = screen;
}
Code:
Object B
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "screen")
public Set<ScreensetDefinition> getScreensetDefinitions()
{
return this.screensetDefinitions;
}
public void setScreensetDefinitions(Set<ScreensetDefinition> screensetDefinitions)
{
this.screensetDefinitions = screensetDefinitions;
}