Hibernate version: 3.2.4.sp1
Name and version of the database you are using: Oracle 10g
I have a stange loading issue with hibernate entity manager (jboss 4.2.2GA).
I have a many to one bi-directional relationship set up:
Code:
GameGroup.java:
...
@OneToMany(fetch = FetchType.EAGER, mappedBy="gameGroup", cascade = CascadeType.ALL)
private Set<GameSubGroup> gameSubGroups = new HashSet<GameSubGroup>();
...
GameSubGroup.java
...
@ManyToOne(optional=false)
@JoinColumn(name="gameGroupId")
private GameGroup gameGroup;
...
and I have used the UUID approach to equals and hashCode which works an absolute treat. I have a previously created a GameGroup that has two GameSubGroups and when you do this (from a clean entity manager):
Code:
GameSubGroup gameSubGroup = entityManager.find(GameSubGroup.class, gameSubGroupId);
This:
Code:
gameSubGroup.getGameGroup().getGameSubGroups().contains(gameSubGroup);
resolves to false...
I've debugged it and the getGameSubGroups() collection does contain the gameSubGroup as expected (same Id and UUID).
I've traced through the code and I've found that when the entityManager is looking up the GameSubGroup two calls are made to hashCode with empty (newly instantiated) GameSubGroup's. It looks as if these newly created objects are being added to the GameGroup's HashSet before they have been loaded with the data from the DB (and hence the UUID). This also means that calling remove() on the GameGroup's collection with a GameSubGroup fails (which is how I found the problem).
You can work around the problem by looking up the owning GameGroup first in effect pre-loading the entity manager.
Is this a bug with loading from the non-owning side of a relationship or have I missed something?
[/code]