-->
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.  [ 4 posts ] 
Author Message
 Post subject: SerializationException on querying Map with enum as key
PostPosted: Fri Mar 28, 2008 9:15 am 
Newbie

Joined: Mon Aug 14, 2006 8:06 pm
Posts: 18
Location: Montréal, Québec
Hibernate version: 3.2
Hibernate-annotations: 3.3.1.GA

I get a SerializationException when I try to access a Map collection whose key is an enum persisted as the enum STRING.

I have a regions table, each region having its ID; for instance, USA is 1. I also have a regions_l table (linked by foreign key) containing the localized name of each country in all possible languages; in the case of the USA, this localized regions_l table would contain this:

Code:
regions_l
id | lang | name
----+------+-----------------------------------
1  | 'en' | 'United States of America'
1  | 'fr' | 'Etats-Unis d'Amérique'
1  | 'de' | 'Vereinigten Staaten von Amerika'


As you can see, the lang field is stored as a two-character String.

Here is the relevant bits of the corresponding annotated Region object:

Code:
public enum Language {
   en, fr, de;
}

@Entity @Table(name="regions")
public class Region implements java.io.Serializable {
   private static final long serialVersionUID = -7244316928277518004L;

   private int id;
   private Map<Language, String> regionL = new EnumMap<Language,String>();

   @Id
   public int getId() { return this.id; }
   public void setId(Integer id) { this.id = id; }

   @CollectionOfElements @JoinTable(name="regions_l", joinColumns={@JoinColumn(name="region")})
   @org.hibernate.annotations.MapKey(columns={@Column(name="lang")}) @Column(name="name")
   public Map<Language, String> getRegionL() { return this.regionL; }
   public void setRegionL(Map<Language, String> regionL) { this.regionL = regionL; }

   @Transient
   public String getName(Language lang) {
      return regionL.get(lang);
   }

}


I would dearly like to annotate that the MapKey is to be interpreted as an enum-string: @Enumerated(EnumType.STRING), but I don't know where to fit that annotation in this particular context!!!

Instead, if I simply call
Code:
getName(Language.en)
I get the following exception:
Code:
org.hibernate.type.SerializationException: could not deserialize
   org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:217)
   org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:240)
   org.hibernate.type.SerializableType.fromBytes(SerializableType.java:82)
   org.hibernate.type.SerializableType.get(SerializableType.java:39)
   org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
   org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
   org.hibernate.persister.collection.AbstractCollectionPersister.readIndex(AbstractCollectionPersister.java:708)
   org.hibernate.collection.PersistentMap.readFrom(PersistentMap.java:258)
   org.hibernate.loader.Loader.readCollectionElement(Loader.java:1008)
   org.hibernate.loader.Loader.readCollectionElements(Loader.java:646)
   org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:591)
   org.hibernate.loader.Loader.doQuery(Loader.java:701)
   org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
   org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
   org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
   org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
   org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:63)
   org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
   org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
   org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
   org.hibernate.collection.AbstractPersistentCollection.readElementByIndex(AbstractPersistentCollection.java:161)
   org.hibernate.collection.PersistentMap.get(PersistentMap.java:146)
   com.mywebsite.Region.getName(Region.java:92)
...
Caused by: java.io.EOFException
   at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)
   at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2748)
   at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:781)
   at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
   at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:252)
   at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:209)
   ... 76 more


Last edited by pierdeux on Fri Mar 28, 2008 1:24 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 11:22 am 
Newbie

Joined: Mon Aug 14, 2006 8:06 pm
Posts: 18
Location: Montréal, Québec
Strange:

I don't get any exception when, instead of Annotations 3.3.1.GA, I use Annotations 3.3.0.GA!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 8:47 am 
Newbie

Joined: Mon May 05, 2008 8:42 am
Posts: 1
Same problem for me.
I'll downgrade Hibernate-Annotations.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 31, 2008 2:58 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
Same problem here. I set @Enumerated(value=EnumType.STRING) and it worked on the map key with Annotations 3.3.0. But this morning I upgraded to 3.3.1 and started to get serialization errors. I think the problem is that string-based enum map keys aren't really supported. There is a JIRA issue open for it. Cast your vote, or better yet, offer a patch. In the meantime, I guess I will downgrade.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.