There is a
very bad bug with deleting from an ordered @OneToMany List.
I have the following:
Code:
@OneToMany(fetch=FetchType.EAGER)
@Cascade(value={CascadeType.ALL, CascadeType.DELETE_ORPHAN})
@IndexColumn(name="submenuIndex")
public List<Menu> getSubMenus()
{
if (subMenus == null)
subMenus = new ArrayList<Menu>();
return subMenus;
}
public void setSubMenus(List<Menu> subMenus)
{
this.subMenus = subMenus;
}
If I ever do
Code:
myMenu.getSubmenus().remove(0);
myEntityManager.flush();
I get
Code:
07:32:09,296 INFO [fcl] Removing old menu entry 186: MenuOption:Application Classes (AppClass) from Menu:App. Data
07:32:09,296 INFO [fcl] Updating Menu:App. Data with submenu changes...
07:32:09,296 INFO [fcl] greenfield/admin is about to update Menu:App. Data
07:32:09,359 INFO [fcl] greenfield/admin is about to update Menu:Static Data
07:32:11,062 INFO [fcl] greenfield/admin is about to update MenuOption:Application Classes (AppClass)
07:32:11,062 INFO [fcl] greenfield/admin is about to update MenuOption:Tax Number Validation Methods (AppTaxNoValidation)
07:32:11,062 INFO [fcl] greenfield/admin is about to update MenuOption:Application Components (AppComponent)
07:32:11,062 INFO [STDOUT] Hibernate: update Menu set parent_uuid=?, version=?, description=?, creatingUser=?, createdDateTime=?, modifiedDateTime=?, modifyingUser=?, houseComponent_uuid=? where uuid=? and version=?
07:32:11,078 INFO [STDOUT] Hibernate: update Menu set parent_uuid=?, version=?, description=?, creatingUser=?, createdDateTime=?, modifiedDateTime=?, modifyingUser=?, houseComponent_uuid=? where uuid=? and version=?
07:32:11,078 INFO [STDOUT] Hibernate: update Menu set parent_uuid=?, version=?, description=?, creatingUser=?, createdDateTime=?, modifiedDateTime=?, modifyingUser=?, houseComponent_uuid=? where uuid=? and version=?
07:32:11,078 INFO [STDOUT] Hibernate: update Menu set parent_uuid=?, version=?, description=?, creatingUser=?, createdDateTime=?, modifiedDateTime=?, modifyingUser=?, houseComponent_uuid=? where uuid=? and version=?
07:32:11,078 INFO [STDOUT] Hibernate: update Menu set parent_uuid=?, version=?, description=?, creatingUser=?, createdDateTime=?, modifiedDateTime=?, modifyingUser=?, houseComponent_uuid=? where uuid=? and version=?
07:32:11,078 INFO [STDOUT] Hibernate: delete from Menu_subMenus where Menu_uuid=? and submenuIndex=?
07:32:11,093 INFO [STDOUT] Hibernate: update Menu_subMenus set subMenus_uuid=? where Menu_uuid=? and submenuIndex=?
07:32:11,093 WARN [JDBCExceptionReporter] SQL Error: 1062, SQLState: 23000
07:32:11,687 ERROR [JDBCExceptionReporter] Duplicate key or integrity constraint violation message from server: "Duplicate entry '209' for key 1"
From the mysql log:
Code:
3 Query delete from Menu_subMenus where Menu_uuid=74 and submenuIndex=2
3 Query update Menu_subMenus set subMenus_uuid=209 where Menu_uuid=74 and submenuIndex=0
3 Query update Menu_subMenus set subMenus_uuid=188 where Menu_uuid=74 and submenuIndex=1
So what it's doing when List elements are removed, is just deleting excess from the end of the link table - in this case, removing element 2. That's OK.
And then going down the remaining link rows (0 and 1), changing them to point to the new values.
Unfortunately, it (Hibernate) created the link table with a "Unique" constraint on the child key, so the set of "subMenus_uuid" to 209 fails at submenuIndex=0 because this already exists at submenuIndex=1.
It's a
BUG and it's a showstopper.