1 Reply Latest reply on May 4, 2009 4:46 AM by a a

    Blob to String mapping

    a a Newbie

      Hello,
      I'm using Hibernate as JPA provider and I'm trying to map clob field form database to String attribute in entity (JPA spec allows this).

      Declaration in entity:

      @Lob
      String name;


      Declaration in database table:
      name varchar(max),


      But when I tried to read entity I got following exceptions (depending on jdbc driver I used):

      Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from varchar to BLOB is unsupported.
      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
      at com.microsoft.sqlserver.jdbc.DataTypes.throwConversionError(DataTypes.java:946)
      at com.microsoft.sqlserver.jdbc.ServerDTVImpl.getValue(dtv.java:1952)
      at com.microsoft.sqlserver.jdbc.DTV.getValue(dtv.java:175)
      at com.microsoft.sqlserver.jdbc.Column.getValue(Column.java:113)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:1982)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:1967)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getBlob(SQLServerResultSet.java:2444)
      at org.hibernate.type.BlobType.get(BlobType.java:80)
      at org.hibernate.type.BlobType.nullSafeGet(BlobType.java:134)
      at org.hibernate.loader.hql.QueryLoader.getResultColumnOrRow(QueryLoader.java:356)
      at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:629)
      at org.hibernate.loader.Loader.doQuery(Loader.java:724)
      at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
      at org.hibernate.loader.Loader.doList(Loader.java:2228)

      or

      org.hibernate.PropertyAccessException: could not set a field value by reflection setter of model.Entity.name
      javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of model.Entity.name
      at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
      at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76)
      at TestTest.test3(TestTest.java:66)
      Caused by: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of model.Entity.name
      at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:114)
      at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:360)
      at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:221)
      at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3594)
      at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:152)
      at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
      at org.hibernate.loader.Loader.doQuery(Loader.java:752)
      at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
      at org.hibernate.loader.Loader.doList(Loader.java:2228)
      at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
      at org.hibernate.loader.Loader.list(Loader.java:2120)
      at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
      at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
      at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
      at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
      at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
      at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
      Caused by: java.lang.IllegalArgumentException: Can not set int field model.Entity.name to org.hibernate.lob.SerializableBlob
      at java.lang.reflect.Field.set(Field.java:657)
      at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:102)

      Which suggests that hibernate doesn't support this mapping. Is it true? Is there any clean solution to this problem or I have to use some kind of workaround?

      My environment:
      MS SQL Server 2005, Hibernate (3.3.1.GA), JDBC Driver: JTDS (1.2.2) or Microsoft SQL Driver (2.0).

      Greetings
      Slawek