I just upgraded to hibernate 3.2 GA with annotations and entitymanager 3.2 GA. Below is a simple comparison of using hibernate's api vs. EJB3 api. Using hibernate api works, using EJB3 results in PersistentObjectException ("detached entity passed to persist")
I have been using EJB 3 annotations from the start with the hibernate api. The only difference in my code is below.
First using hte EJB 3 api fails(what am I doing wrong here?).....
Code:
private ProjectDto updateOrSave(ProjectDto projectDto)
{
ProjectDBO projDbo = convert(projectDto);
EntityManager session = openSession();
try {
session.getTransaction().begin();
FolderDBO folder = session.find(FolderDBO.class, projectDto.getFolderId());
if(folder == null) {
throw new IllegalArgumentException("Could not find folder with primary key="+projectDto.getFolderId());
}
folder.addChildProject(projDbo);
projDbo = session.merge(projDbo);
session.getTransaction().commit();
} finally {
session.close();
}
return convert(projDbo);
}
Using the hibernate api works just fine......
Code:
private ProjectDto updateOrSave(ProjectDto projectDto)
{
ProjectDBO projDbo = convert(projectDto);
EntityManager session = openSession();
try {
session.getTransaction().begin();
Session hibSession = (Session)session.getDelegate();
FolderDBO folder = (FolderDBO)hibSession.get(FolderDBO.class, projectDto.getFolderId());
if(folder == null) {
throw new IllegalArgumentException("Could not find folder with primary key="+projectDto.getFolderId());
}
folder.addChildProject(projDbo);
hibSession.saveOrUpdate(projDbo);
session.getTransaction().commit();
} finally {
session.close();
}
return convert(projDbo);
}
any help is greatly appreciated.
thx,
dean