1 Reply Latest reply on Oct 7, 2005 3:08 PM by epbernard

    @Lob and ClassCastException

    jc7442

      I have the following code:

      @Embeddable(access = AccessType.FIELD)
      public final class AuditInfo implements Serializable {
       @Lob
       private Map<String, String> data = new HashMap<String, String>();
      


      When an instance is persisted I have the following exception:
      Exception in thread "main" java.lang.ClassCastException: java.util.HashMap
       at org.hibernate.type.BlobType.set(BlobType.java:49)
       at org.hibernate.type.BlobType.nullSafeSet(BlobType.java:122)
       at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:261)
       at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1816)
       at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1793)
       at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1940)
       at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2395)
       at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
       at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)


      To fix that I need to change my code to:
      @Embeddable(access = AccessType.FIELD)
      public final class AuditInfo implements Serializable {
       @Lob
       private HashMap<String, String> data = new HashMap<String, String>();
      


      In fact Map interfave is not serializable and HashMap class is serializable.

      Usually in the implementation we prefer expose interface and not the implemntation. Since hibernate uses the type of the field and not the type of the instance (I suppose), it is not possible to expose only interfaces.

      In EJB3 persistence public draft P144, it is written:
      Blob fields may be defined to be of type Byte[] or a Serializable type.


      Is is a bug, a known issue, ... ?