0 Replies Latest reply on Aug 16, 2011 7:45 PM by Joseph Hwang

    org.hibernate.TransientObjectException?

    Joseph Hwang Novice

      Pls, Check my codes. I made two Entity Beans.

       

      -Members.java

       

      package com.aaa.ejb3;

       

      import java.io.Serializable;

      import java.util.ArrayList;

      import java.util.Collection;

       

      import javax.persistence.Column;

      import javax.persistence.Entity;

      import javax.persistence.FetchType;

      import javax.persistence.Id;

      import javax.persistence.JoinColumn;

      import javax.persistence.JoinTable;

      import javax.persistence.OneToMany;

      import javax.persistence.Table;

       

      @Entity

      @Table(name = "family")

      public class Members implements Serializable {

      @Id

      @Column(name = "EMP_ID")

      private String id ;

       

      @Column(name = "EMP_Passwd")

      private String passwd ;

       

      @Column(name = "EMP_Name")

      private String name ;

       

      @OneToMany(fetch=FetchType.EAGER)
      @JoinTable(name="info", joinColumns = {@JoinColumn(name = "EMP_Name")})
      private Collection<Records> record = new ArrayList();
      ....

       

      }
       

      - Records.java

       

      package com.aaa.ejb3;

       

      import java.io.Serializable;

       

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.Table;

       

      @Entity
      @Table(name = "info")
      public class Records implements Serializable {
       

      @Id
      @GeneratedValue(strategy=GenerationType.TABLE)
      @Column(name = "EMP_ID")
      private int i;
       

      @Column(name = "EMP_Name")
      private String name;

       

      ....

       

      }

       

       

      and I added EntityManager in EJB Bean like below

       

       

      -MappingTestBean.java

       

      package com.aaa.ejb3;

       

      import java.util.Collection;

       

      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;

       

      @Stateless
      public class MappingTestBean implements IMappingTestPort {
       

      @PersistenceContext(unitName="MyFamily")
      EntityManager em;

       

      public Collection<Records> getFamilyMember(String name) {
      // TODO Auto-generated method stub
      Members m = (Members)em.find(Members.class, name);
      Collection<Records> records = m.getRecord();
       

      return records;
      }

       

      public void setFamilyMember(Members m){
      em.persist(m);
      }

      }

       

       

      Deployment of EJB3 Beans are successfull.

       

       

       

      In JSP I invoke the method

       

      try {
      Context ctx = new InitialContext();
      IMappingTestPort port = (IMappingTestPort) ctx.lookup("MappingTestBean/remote");
       

      Records r = new Records();
      r.setName("aaa");
      r.setAccount("111-2222");
      r.setHobby("bbb");
      r.setPhone(021112222);
       

      Members m = new Members();
      m.setId("aupres");
      m.setPasswd("aaa");
      m.setName("vvv");
      m.getRecord().add(r);
       

      port.setFamilyMember(m);

       

       

      But Exception Occurs. The Exception is

       

       

      20:02:43,005 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
      org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.aaa.ejb3.Records
      at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
      at org.hibernate.type.EntityType.getIdentifier(EntityType.java:430)
      at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:101)
      at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:777)
      at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1165)
      at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
      at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
      at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:101)
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:269)
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:89)
      at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
      at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1423)
      at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:137)
      at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
      at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:170)
      at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
      at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:190)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:421)
      at org.jboss.ejb3.remoting.IsLocalInterceptor.invokeLocal(IsLocalInterceptor.java:85)
      at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:72)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.aspects.remoting.PojiProxy.invoke(PojiProxy.java:62)
      at $Proxy310.invoke(Unknown Source)
      at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:207)
      at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:164)
      at $Proxy309.setFamilyMember(Unknown Source)
      at org.apache.jsp.index_jsp._jspService(index_jsp.java:92)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
      at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
      at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
      at java.lang.Thread.run(Thread.java:662)

       

       

      I need your advice! Thanks in advance.

       

       

      Best Regards.