-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Deserialization Exception when mapping to arrays in entities
PostPosted: Wed Apr 23, 2008 7:56 am 
Newbie

Joined: Wed Apr 23, 2008 7:10 am
Posts: 2
Hi everyone!

I'm using a generic object which contains an ID, a name and an Object[].

Code:
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Immutable
public class MyGenericObject {

   @Id
   public Long getId();
   public void setId(Long theID);

   @Basic(optional = true)
   Object[] getData();
   void setData(Object[] data);

   String getName();
   void setName(String name);
}


Now, I want to fill the object with a native query:
Code:
entityManager.createNativeQuery(
   "SELECT lex_fk AS id, lex_name AS name, lex_min as data{0}, lex_max as data{1} FROM `tblex` WHERE lex_name <> '';",
   MyGenericObject.class).getResultList();


As you can see, lex_min should be the first element of the MyGenericObject.data Array, lex_max the second. I'm not sure if the syntax I use is correct, but I've seen it in other posts. Anything else I tried didn't even get me close to the result I see now.

Well, anyways it doesn't work. Here's the stacktrace:

Code:
javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:75)
   at ch.....getLex(...DAOImpl.java:77)
   at ch.....ServiceImpl.getLex(...ServiceImpl.java:45)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:296)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:177)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at $Proxy18.getLeadershipExperience(Unknown Source)
   at ch.....HibernateTest.TestStaticTables(...HibernateTest.java:86)
Caused by: org.hibernate.type.SerializationException: could not deserialize
   at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:217)
   at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:240)
   at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:82)
   at org.hibernate.type.SerializableType.get(SerializableType.java:39)
   at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
   at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
   at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
   at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2096)
   at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)
   at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)
   at org.hibernate.loader.Loader.getRow(Loader.java:1206)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
   at org.hibernate.loader.Loader.doQuery(Loader.java:701)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
   at org.hibernate.loader.Loader.doList(Loader.java:2220)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
   at org.hibernate.loader.Loader.list(Loader.java:2099)
   at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
   at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
   at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
   at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:66)
   ... 36 more
Caused by: java.io.EOFException
   at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
   at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
   at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
   at java.io.ObjectInputStream.<init>(Unknown Source)
   at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:252)
   at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:209)


Can anyone help me out here or point me to the relevant section of the documentation? So far my search for this didn't yield any results...

Thanks in advance,
Jan-Philipp


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 10:10 am 
Newbie

Joined: Wed Apr 23, 2008 7:10 am
Posts: 2
I still don't know if there is any way to do this automatically, but I have solved the problem by creating a custom mapping like this:

Code:
@SqlResultSetMappings({
   @SqlResultSetMapping(
      name = "MyGenericObjectDataMapping",
      entities = {
         @EntityResult(entityClass = MyGenericObject.class)
      },
      columns = {
         @ColumnResult(name = "data1"),
         @ColumnResult(name = "data2")
      }
   )})


and using a custom conversion method to put the data where it belongs:

Code:
private List<MyGenericObject> createMyGenericObjectList(List<Object[]> results) {
   List<MyGenericObject> genObjects = new ArrayList<MyGenericObject>();
   for (Object[] result : results) {
      MyGenericObject genObject = (MyGenericObject)result[0];
      if (genObject!= null) {
         Object[] data = new Object[] {result[1], result[2]};
         genObject.setData(data);
         genObjects.add(ontObject);
      }
   }
   return genObjects;
}


I don't think that this is really the way it is supposed to be, but it works. I'd still be thankful if anybody comes up with a better solution.

Best regards,
Jan-Philipp


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.