Exception from handling Maps with MapKey Type of Enum

74 views
Skip to first unread message

javadoc

unread,
Jun 5, 2009, 1:18:11 PM6/5/09
to hibernate-memcached
hi

i ran into a weird problem that is caused by hibernate-memcached.

Mapping is like this:

@Entity class A{
@id long id;

@OneToMany(cascade =
{ CascadeType.ALL }, mappedBy = "a", fetch = FetchType.LAZY)
@MapKey(name = "myenum")
private Map<MyEnum, B> policies = new HashMap<MyEnum, B>();
//...}


which seems to make hibernate put keys in forms of enums to the
2ndlvevl cache.
retrieving them back causes:

org.hibernate.TypeMismatchException: Provided id of the wrong type.
Expected: class java.lang.Integer, got class
de.thomasdaily.project.tdpublic.enums.FieldGroup
at org.hibernate.event.def.DefaultLoadEventListener.onLoad
(DefaultLoadEventListener.java:86)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:882)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:
850)
at org.hibernate.type.EntityType.resolveIdentifier
(EntityType.java:557)
at org.hibernate.type.ManyToOneType.assemble(ManyToOneType.java:
196)
at org.hibernate.collection.PersistentSet.initializeFromCache
(PersistentSet.java:124)
at org.hibernate.cache.entry.CollectionCacheEntry.assemble
(CollectionCacheEntry.java:35)
at
org.hibernate.event.def.DefaultInitializeCollectionEventListener.initializeCollectionFromCache
(DefaultInitializeCollectionEventListener.java:136)
at
org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection
(DefaultInitializeCollectionEventListener.java:48)
at org.hibernate.impl.SessionImpl.initializeCollection
(SessionImpl.java:1720)
at
org.hibernate.collection.AbstractPersistentCollection.initialize
(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read
(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.toArray
(PersistentSet.java:171)

using any other kind of 2nd level cache works fine, which is why i
suspect hibernate memcached.

what can i do / provide to help reproducing or fixing this issue?

cu uwe

Ray Krueger

unread,
Jun 5, 2009, 1:55:12 PM6/5/09
to hibernate...@googlegroups.com

If you switch your second level cache to
org.hibernate.cache.HashtableCacheProvider do you still get the same
error?
I'm having a hard time seeing how hibernate-memcached is causing this.
I don't even see hibernate-memcached in the stack trace for example.
Let's see if we can get to the bottom of this. I've honestly never
persisted a map or used the @MapKey annotation at all.

Can you try this again with the HashtableCacheProvider if you haven't already?

Do you already have a "Type" annotation setup somewhere that defines
the mapping for that enum you're using? JPA/Hibernate won't handle
Enums by default without mapping out the type.

javadoc

unread,
Jun 5, 2009, 7:47:21 PM6/5/09
to hibernate-memcached
hi

> > what can i do / provide to help reproducing or fixing this issue?
> If you switch your second level cache to
> org.hibernate.cache.HashtableCacheProvider do you still get the same
> error?

no. neither with hastable, nor ehcache show the problem and this
mapping is in production (with EHCache) for 6 months now.

> I'm having a hard time seeing how hibernate-memcached is causing this.
> I don't even see hibernate-memcached in the stack trace for example.

my "feeling" is, that the cache provides the result in another way
than the others do.
i´ll try to wrap the cache and see what is passed in and out and
compare this to a mapped hashtable-provider.

> Do you already have a "Type" annotation setup somewhere that defines
> the mapping for that enum you're using? JPA/Hibernate won't handle
> Enums by default without mapping out the type.

on the "peer" entity, it is declared to be stored as String (which we
do for all our entities)

i´ll provide a complete example lateron (don´t have to sources at hand
right now).
is there an archetype for creating a quickstart app to demonstrate
this problem?

thanks, uwe

javadoc

unread,
Jun 5, 2009, 7:49:29 PM6/5/09
to hibernate-memcached
correction:

> it is declared to be stored as String (which we
> do for all our entities)

should have been:

it is declared to be stored as String (which we do for all our enums)

Ray Krueger

unread,
Jun 5, 2009, 9:18:01 PM6/5/09
to hibernate...@googlegroups.com

Maybe pull down the hibernate-memcached source and debug through it to
see what gets cached.
It's really weird because hibernate-memcached doesn't look at what's
being cached at all. It just takes what hibernate gives it and puts in
memcached.

Though just now I'm realizing I did see some weirdness with memcached
wherein you put an int into the cache and get back a string. That
definitely isn't the case here though as you're seeing a class/string
ClassCastException. Very weird.

Reply all
Reply to author
Forward
0 new messages