I have upgraded my development environment to [JBoss EJB3 RC1][1]. This is apparently more strict and correct according to the [EJB3 spec][2]. This is great, because it helps fixing all that “ugly code ™”. In the next couply of posts I’ll be addressing some of the funny little Exceptions, which I receive while converting the existing [FriFinans][3] code into shiny pebbles
.
The first one is
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
…
at org.frifinans.server.stateless.UserSettingsBean.get()Lorg.frifinans.server.entity.LedgerUserSetting;(Unknown Source)
…
This one has a simple solution. I was trying to cascade persist an object, which was already persisted. This is a little piece of the [UserSettingsBean][4] SessionBean, which tried to persist:
public LedgerUserSetting get () {
LedgerUserSetting lus = new LedgerUserSetting ();
CommonUser user = ur.getCurrent ();
if (user != null) {
// Find user setting
LedgerUserSetting l = null;
try {
l = (LedgerUserSetting) manager.
createQuery (
"FROM " +
" LedgerUserSetting lus " +
"WHERE " +
" lus.user = :user ").
setParameter ("user", user).
getSingleResult ();
}
catch (EntityNotFoundException e) {
// Ok
}
if (l != null) {
lus = l;
}
else {
lus.setUser (user);
manager.persist(lus); // < --- Try to persist (this is ok)
}
}
return lus;
}
The problem was in the [LedgerUserSetting][5] EntityBean, where this following code caused the Exception:
@OneToOne (cascade = CascadeType.PERSIST) // We shouldn't cascade
@JoinColumn (name = "username")
public CommonUser getUser () {
return user;
}
Remove the cascade option and we end up with:
@OneToOne // Thats more like it.
@JoinColumn (name = "username")
public CommonUser getUser () {
return user;
}
[1]: https://sourceforge.net/project/showfiles.php?group_id=22866&package_id=132063&release_id=346762
[2]: http://www.jcp.org/en/jsr/detail?id=220
[3]: http://cvs.frifinans.dk:8080/viewrep/FriFinans/frifinans
[4]: http://cvs.frifinans.dk:8080/viewrep/FriFinans/frifinans/src/org/frifinans/server/stateless/UserSettingsBean.java?r=1.6
[5]: http://cvs.frifinans.dk:8080/viewrep/FriFinans/frifinans/src/org/frifinans/server/entity/LedgerUserSetting.java?r=1.5