4 Replies Latest reply on Aug 15, 2006 6:02 AM by justinb

    ClassCastException on OneToOne merge

    justinb Newbie

      I have a OneToOne relationship as follows:

      package company.payroll
      ...
      @Entity
      @Inheritance(strategy=InheritanceType.JOINED)
      @Table(name="core_employee")
      public class Employee extends Person {
       ...
       private Employment employment;
       ...
       @OneToOne(
       optional=false,
       cascade={CascadeType.ALL})
       @JoinColumn(
       name="employment",
       unique=true,
       nullable=false,
       updatable=true)
       public Employment getEmployment() {
       return employment;
       }
      
       public void setEmployment(Employment employment) {
       this.employment = employment;
       }
       ...
      }


      and

      package company.payroll
      ...
      @Entity
      @Table(name = "payroll_employment")
      @SequenceGenerator(
       name = "payroll.Employment.id",
       sequenceName= "payroll_employment_id")
      public class Employment implements Serializable {
       ...
       private Integer id;
      
       private Employee employee;
       ...
       @Id(
       generate = GeneratorType.SEQUENCE,
       generator = "payroll.Employment.id")
       public Integer getId() {
       return id;
       }
      
       public void setId(Integer id) {
       this.id = id;
       }
      
       @OneToOne(mappedBy = "employment")
       public Employee getEmployee() {
       return employee;
       }
      
       public void setEmployee(Employee employee) {
       this.employee = employee;
       }
       ...
      }


      This all works fine (great, in fact) when creating a new Employee, and deleting an existing Employee. But when updating an existing Employee like so:

      employee = entityManager.merge(employee);


      I get the following Exception:
      10:44:12,806 ERROR [MakeEmployeePersistentAction] Error updating company.payroll.Employee
      javax.ejb.EJBException: null; CausedByException is:
       company.payroll.Employment
       at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:70)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:61)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:32)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:91)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:163)
       at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:60)
       at $Proxy406.makePersistent(Unknown Source)
       at company.servlet.action.dao.MakePersistentAction.execute(MakePersistentAction.java:35)
       at company.payroll.MakeEmployeePersistentAction.execute(MakeEmployeePersistentAction.java:32)
       at company.payroll.MakeEmployeePersistentAction.execute(MakeEmployeePersistentAction.java:1)
       at company.servlet.action.dao.DAOAction.process(DAOAction.java:35)
       at company.servlet.action.dao.DAOAction.process(DAOAction.java:1)
       at company.servlet.action.StatelessClientAction.process(StatelessClientAction.java:28)
       at company.servlet.action.TypicalAction.execute(TypicalAction.java:27)
       at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
       at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
       at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
       at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
       at company.servlet.action.CustomActionServlet.doPost(CustomActionServlet.java:37)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at company.links.LinksFilter.doHttp(LinksFilter.java:58)
       at company.servlet.filters.CustomFilter.doFilter(CustomFilter.java:33)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at company.security.UserFilter.doHttp(UserFilter.java:23)
       at company.servlet.filters.CustomFilter.doFilter(CustomFilter.java:33)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at company.thoughts.ThoughtFilter.doHttp(ThoughtFilter.java:50)
       at company.servlet.filters.CustomFilter.doFilter(CustomFilter.java:33)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
       at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:407)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:365)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
       at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
       at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
       at java.lang.Thread.run(Thread.java:595)
      java.lang.ClassCastException: company.payroll.Employment
       at org.hibernate.type.IntegerType.set(IntegerType.java:41)
       at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:63)
       at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:45)
       at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1514)
       at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1576)
       at org.hibernate.loader.Loader.doQuery(Loader.java:661)
       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
       at org.hibernate.loader.Loader.loadEntity(Loader.java:1782)
       at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:47)
       at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:85)
       at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1512)
       at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:365)
       at org.hibernate.type.EntityType.resolve(EntityType.java:306)
       at org.hibernate.type.EntityType.replace(EntityType.java:207)
       at org.hibernate.type.TypeFactory.replace(TypeFactory.java:431)
       at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:279)
       at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:245)
       at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:102)
       at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:616)
       at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:602)
       at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:156)
       at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
       at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
       at org.hibernate.event.def.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:330)
       at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:244)
       at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:102)
       at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
       at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:608)
       at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:594)
       at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:140)
       at org.jboss.ejb3.entity.InjectedEntityManager.merge(InjectedEntityManager.java:102)
       at company.ejb.DAOBeanCMP.makePersistent(DAOBeanCMP.java:122)
       at company.payroll.EmployeeDAOBean.makePersistent(EmployeeDAOBean.java:59)
       at company.payroll.EmployeeDAOBean.makePersistent(EmployeeDAOBean.java:1)
       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 org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:109)
       at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:32)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:66)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:61)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:32)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:91)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:163)
       at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:60)
       at $Proxy406.makePersistent(Unknown Source)
       at company.servlet.action.dao.MakePersistentAction.execute(MakePersistentAction.java:35)
       at company.payroll.MakeEmployeePersistentAction.execute(MakeEmployeePersistentAction.java:32)
       at company.payroll.MakeEmployeePersistentAction.execute(MakeEmployeePersistentAction.java:1)
       at company.servlet.action.dao.DAOAction.process(DAOAction.java:35)
       at company.servlet.action.dao.DAOAction.process(DAOAction.java:1)
       at company.servlet.action.StatelessClientAction.process(StatelessClientAction.java:28)
       at company.servlet.action.TypicalAction.execute(TypicalAction.java:27)
       at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
       at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
       at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
       at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
       at company.servlet.action.CustomActionServlet.doPost(CustomActionServlet.java:37)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at company.links.LinksFilter.doHttp(LinksFilter.java:58)
       at company.servlet.filters.CustomFilter.doFilter(CustomFilter.java:33)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at company.security.UserFilter.doHttp(UserFilter.java:23)
       at company.servlet.filters.CustomFilter.doFilter(CustomFilter.java:33)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at company.thoughts.ThoughtFilter.doHttp(ThoughtFilter.java:50)
       at company.servlet.filters.CustomFilter.doFilter(CustomFilter.java:33)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
       at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:407)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:365)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
       at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
       at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
       at java.lang.Thread.run(Thread.java:595)


      It seems that somewhere along the line, Hibernate is trying to cast the entire Employment object as its id? What am I doing wrong? (This is the first time I'm trying to employ the OneToOne relationship.)

        • 1. Re: ClassCastException on OneToOne merge
          John Blackmore Newbie

          I suspect it's doing that because your JoinColumn is 'employment' rather than 'employment_id'.

          What's the @Id of Employee/Person?

          • 2. Re: ClassCastException on OneToOne merge
            justinb Newbie

            Hi John,

            Thanks for your reply. Person's Id is "id" and it is a String. I was under the impression that the id's of the one-to-one would be independant of each other if @PrimaryKeyJoinColumn was not used.

            So I tried using the @PrimaryKeyJoinColumn approach instead (took a bit of re-structuring) and it works just fine now.

            Thanks,
            Justin

            • 3. Re: ClassCastException on OneToOne merge
              Novice

              Justin,

              I am seeing exactly the same problem in my application with 4.0.4 and EJB-3RC8. Could you please outline how exactly you solved the problem?

              • 4. Re: ClassCastException on OneToOne merge
                justinb Newbie

                I had a look and it seems I went against autogenerating the Employment id and instead made the Employment id be the same (String) as the Employee id.

                I dropped the @SequenceGenerator on the Employment side and had the following in the Employee class:

                @OneToOne(optional = false, cascade = {CascadeType.ALL})
                @PrimaryKeyJoinColumn
                public Employment getEmployment() {
                 return employment;
                }


                Then, when ever I make the Employee persistent, I just first make sure that the Employment is not null, and has the same id as the Employee.

                I think that was a gross work around, but at the time I needed to get it working.

                Hope that helps,
                Justin