4 Replies Latest reply on Aug 22, 2007 6:26 AM by Francesco Benvegna

    ClassCastException when trying to merge an entity bean

    Patricio Munoz Newbie

      I get a ClassCastException when trying to merge a bean of this class:

      package hormigaclub.entidades;

      import javax.persistence.*;

      @Entity(access = AccessType.FIELD)
      @Table(name = "personas")
      public class Persona extends Entidad {
      private static final long serialVersionUID = 1L;

      @Id(generate = GeneratorType.AUTO)
      private Long id;

      private Long dni;
      private String apellido;
      private String nombre1;
      private String nombre2;
      private String direccionCalle;
      private Integer direccionNumero;
      private String direccionDpto;
      private String direccionLocalidad;
      private Integer direccionCodPostal;
      private String direccionProvincia;
      private String telefono1;
      private String telefono2;
      private String email;

      @OneToOne(mappedBy = "persona")
      private Cliente cliente;

      @OneToOne(mappedBy = "persona")
      private Promotor promotor;

      @OneToOne(mappedBy = "persona")
      private Empleado empleado;

      @OneToOne(mappedBy = "persona")
      private Proveedor proveedor;

      (various methods)....

      }

      the stacktrace is the following :

      2005-09-23 14:20:26,383 DEBUG [org.hibernate.jdbc.AbstractBatcher] preparing statement
      2005-09-23 14:20:26,383 DEBUG [org.hibernate.type.LongType] binding 'hormigaclub.entidades.Persona@19747ae' to parameter: 1
      2005-09-23 14:20:26,383 INFO [STDOUT] java.lang.ClassCastException: hormigaclub.entidades.Persona

      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.type.LongType.set(LongType.java:40)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:63)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:45)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1456)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1518)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.loader.Loader.doQuery(Loader.java:638)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:221)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.loader.Loader.loadEntity(Loader.java:1695)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:46)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:85)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1395)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:356)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.type.EntityType.resolve(EntityType.java:297)
      2005-09-23 14:20:26,384 INFO [STDOUT] at org.hibernate.type.EntityType.replace(EntityType.java:206)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.hibernate.type.TypeFactory.replace(TypeFactory.java:407)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:262)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:247)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:111)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:60)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:542)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:140)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.jboss.ejb3.entity.InjectedEntityManager.merge(InjectedEntityManager.java:102)
      2005-09-23 14:20:26,385 INFO [STDOUT] at hormigaclub.beans.AdmUsuariosSBBean.guardarUsuario(AdmUsuariosSBBean.java:59)
      2005-09-23 14:20:26,385 INFO [STDOUT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2005-09-23 14:20:26,385 INFO [STDOUT] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      2005-09-23 14:20:26,385 INFO [STDOUT] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      2005-09-23 14:20:26,385 INFO [STDOUT] at java.lang.reflect.Method.invoke(Method.java:585)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:99)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:32)
      2005-09-23 14:20:26,385 INFO [STDOUT] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)

      it seems that jboss is trying to bind the toString() method of the entity to it's id field (parameter '1') which is of type Long.
      Can anyone tell me why is this happening and how to solve it?
      thank you very much