-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: convering from hibernate api to ejb3 api
PostPosted: Mon Nov 13, 2006 5:29 pm 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
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

_________________
The list of compelling reasons to reduce the estimate does not include you simply wishing it would take less time - Unknown


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 6:08 pm 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Please note that the semantics of merge() are quite different to the semantics of saveOrUpdate(). I'm sure you get the same exception using (hibernate) session.merge(...)

You can find a comparison of these in the reference documentation at
http://www.hibernate.org/hib_docs/v3/re ... veorupdate

Could you please post a stacktrace of your exception?

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 25, 2006 2:27 pm 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
Stack trace is the below. I knew that the semantics of merge was different which is why I had the "projDbo = " in front of the call to merge unlike saveOrUpdate....what else would I be missing so I can basically do a saveOrUpdate kind of call in EJB3?

javax.persistence.RollbackException: Error while commiting the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:63)
at biz.xsoftware.test.project.TestSaveOrUpdate.updateOrSave(TestSaveOrUpdate.java:78)
at biz.xsoftware.test.project.TestSaveOrUpdate.testUpdateChildsFolderAndData(TestSaveOrUpdate.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:165)
at biz.xsoftware.test.project.hibernate.HibTestCase.runTest(HibTestCase.java:62)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:210)
at junit.framework.TestSuite.run(TestSuite.java:205)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: biz.xsoftware.impl.project.svc.db.ProjectDBO
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:644)
at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:636)
at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:323)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:53)
... 18 more

_________________
The list of compelling reasons to reduce the estimate does not include you simply wishing it would take less time - Unknown


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.