-->
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.  [ 7 posts ] 
Author Message
 Post subject: unable to cache class with @Enumerated data member
PostPosted: Wed Apr 19, 2006 2:25 pm 
Newbie

Joined: Tue Jan 17, 2006 5:46 pm
Posts: 7
Hello,

I am having trouble caching Hibernate entities that include Java 1.5 Enum members. I'm able to store, retrieve, update, etc. just fine; but EhCache seems to be choking like so:

java.io.NotSerializableException: org.hibernate.type.EnumType

Caching works for all my classes that don't have Enum types, and doesn't for ones that do. I don't see any errors outside of when I try to .getMemoryStoreSize on the named cache, and even then it's just a logged error- doesn't actually crash, just continues as though the cache doesn't exist. (the above exception is caught by EhCache, then logged)

Originally I hadn't annotated my Enum getter at all and Hibernate seemed to figure it out; since, I've mapped it as @Enumerated(EnumType.ORDINAL), thinking this might fix the caching problem, but it hasn't. Also note I'm using 3.2 with EhCache 1.1, but in fact I've tried every permutation of 3.1/3.2 x Eh1.1/1.2, and all displayed the same behavior.

Has anyone faced a similar problem? I read a bit about EhCache 1.2 not requiring elements to be cached, but it seems Hibernate tries to cast elements to Serializable (cache.put( (Serializable) key, (Serializable) element); ) anyway, so I'm not sure that'll help.

Thanks in advance.
Rogers


Hibernate version: 3.2.0.cr1

Mapping documents:
@Entity
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,
region="yi.revenuetyperevenuedatum")
@IdClass(RevenueTypeRevenueDatum.MyKey.class)
public class RevenueTypeRevenueDatum implements Serializable{
private short revTypeId;

private RevenueType revType;

private Date date;
private TimePeriod.Type timeType;

private double revenue;

public void setPeriod(TimePeriod period){
this.timeType = period.getTimeType();
this.date = period.getDate();
}



@Override
public boolean equals(Object obj) {
RevenueTypeRevenueDatum me = (RevenueTypeRevenueDatum)obj;
return me.revTypeId==revTypeId &&
me.date.equals(date) &&
me.timeType.equals(timeType);
}

@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + revTypeId;
hash = 31 * hash + date.hashCode();
hash = 31 * hash + timeType.hashCode();
return hash;
}

@Id
public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}


@Id
@Enumerated(EnumType.ORDINAL)
public TimePeriod.Type getTimeType() {
return timeType;
}

public void setTimeType(TimePeriod.Type timeType) {
this.timeType = timeType;
}


public double getRevenue() {
return revenue;
}

public void setRevenue(double revenue) {
this.revenue = revenue;
}

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="rev_type_id",insertable=false,updatable=false)
public RevenueType getRevType() {
return revType;
}

public void setRevType(RevenueType revType) {
this.revType = revType;
this.revTypeId = revType.getId();
}


@Id
@Column(name="rev_type_id")
public short getRevTypeId() {
return revTypeId;
}

public void setRevTypeId(short revTypeId) {
this.revTypeId = revTypeId;
}



@Embeddable
public static class MyKey implements Serializable{
private short revTypeId;

private Date date;
private TimePeriod.Type timeType;

@Override
public boolean equals(Object obj) {
MyKey me = (MyKey)obj;
return me.revTypeId==revTypeId &&
me.date.equals(date) &&
me.timeType.equals(timeType);
}

@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + revTypeId;
hash = 31 * hash + date.hashCode();
hash = 31 * hash + timeType.hashCode();
return hash;
}

public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}

@Column(name="rev_type_id")
public short getRevTypeId() {
return revTypeId;
}
public void setRevTypeId(short revTypeId) {
this.revTypeId = revTypeId;
}


@Enumerated(EnumType.ORDINAL)
public TimePeriod.Type getTimeType() {
return timeType;
}
public void setTimeType(TimePeriod.Type timeType) {
this.timeType = timeType;
}

}

}





Code between sessionFactory.openSession() and session.close():

(just checking the ehCache size of named cache inside a CacheManager loop):

thisCache.getMemoryStoreSize();



Full stack trace of any exception that occurs:
Actually does not throw an error (logs an ERROR, see below), but I have isolated the exception that gets caught as the following:

java.io.NotSerializableException: org.hibernate.type.EnumType



Name and version of the database you are using: MySQL 4.1

The generated SQL (show_sql=true):
Not applicable- error happens when trying to cache/serialize.


Debug level Hibernate log excerpt:
ERROR net.sf.ehcache.Element - Error measuring element size for element with key com.jb.app.xxx.xxx.RevenueTypeRevenueDatum#com.jb.app.xxx.xxx.RevenueTypeRevenueDatum$MyKey@3b9f5b94


Top
 Profile  
 
 Post subject: ehcache-1.2 and the new provider do not require Serializable
PostPosted: Wed Apr 19, 2006 6:14 pm 
Newbie

Joined: Tue Oct 21, 2003 7:59 pm
Posts: 9
Hi

ehcache-1.2 and the new provider do not require Serializable.

You can test this out before it gets merged into Hibernate 3.2 by downloading ehcache-1.2rc1 from http://ehcache.sf.net. A candidate Hibernate provider can be found here: http://ehcache.sourceforge.net/EhCache12Provider.java. Change your Hibernate configuration to use that provider.

Greg Luck


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 6:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I actually do not really understand how this could happen, types are not supposed to be serialized.
Do you have the full stack trace?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 7:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hmum, that's actually my bad. I've fixed that in Hibernate Annotations
http://opensource.atlassian.com/projects/hibernate/browse/ANN-319

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 8:15 pm 
Newbie

Joined: Tue Jan 17, 2006 5:46 pm
Posts: 7
gluck-
Thanks for the tip, I am trying right now with the candidate provider you linked. (Having trouble with "diskStorePath is in use by another cache", which may be Spring creating another CacheManager somewhere ... will post back if it works/doesn't work)

emmanuel-
Here is the stacktrace. Sorry for the formatting; as I said, the exception doesn't actually get thrown, EhCache catches it, but this is what the NotSerializable error's stacktrace looks like in the Eclipse debugger:

[java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1075), java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369), java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341), java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284), java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073), java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1245), java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1069), java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369), java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341), java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284), java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073), java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369), java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341), java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284), java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073), java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369), java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341), java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284), java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073), java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:291), net.sf.ehcache.Element.getSerializedSize(Element.java:327), net.sf.ehcache.store.MemoryStore.getSizeInBytes(MemoryStore.java:339), net.sf.ehcache.Cache.calculateInMemorySize(Cache.java:674), com.jb.app.cnet.webapps.yieldinfo.controller.CacheDebugger.dumpCacheStats(CacheDebugger.java:33), sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method), sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39), sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25), java.lang.reflect.Method.invoke(Method.java:585), com.sun.el.parser.AstValue.invoke(AstValue.java:130), com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274), com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68), com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69), org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63), oracle.adf.view.faces.component.UIXCommand.broadcast(UIXCommand.java:202), javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90), javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164), org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:316), org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86), javax.faces.webapp.FacesServlet.service(FacesServlet.java:106), org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252), org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173), org.apache.shale.faces.ShaleApplicationFilter.doFilter(ShaleApplicationFilter.java:285), org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202), org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173), oracle.adfinternal.view.faces.webapp.AdfFacesFilterImpl._invokeDoFilter(AdfFacesFilterImpl.java:279), oracle.adfinternal.view.faces.webapp.AdfFacesFilterImpl._doFilterImpl(AdfFacesFilterImpl.java:248), oracle.adfinternal.view.faces.webapp.AdfFacesFilterImpl.doFilter(AdfFacesFilterImpl.java:220), oracle.adf.view.faces.webapp.AdfFacesFilter.doFilter(AdfFacesFilter.java:80), org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202), org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173), org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174), org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76), org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202), org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173), org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174), org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76), org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202), org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173), org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174), org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76), org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202), org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173), org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213), org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178), org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126), org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105), org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107), org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148), org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869), org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:667), org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527), org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80), org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684), java.lang.Thread.run(Thread.java:595)]


Top
 Profile  
 
 Post subject: Re: ehcache-1.2 and the new provider do not require Serializ
PostPosted: Thu Apr 20, 2006 5:43 pm 
Newbie

Joined: Tue Jan 17, 2006 5:46 pm
Posts: 7
Greg, thanks again for the tip. Couple things:

1) I believe in EhCache12Provider, the start function should be acquiring its manager like so:

Code:
manager = CacheManager.create();


rather than calling the CacheManager constructor. Multiple SessionFactory's in Spring bombed out in the existing code; the .create() method ensures CacheManager is a singleton.

2) Hibernate still can't quite handle non-serializable classes, because org.hibernate.cache.EhCache has the following line in its put(Object key, Object value) method (line 152):

Code:
Element element = new Element( (Serializable) key, (Serializable) value );


The Element constructor in EhCache1.2 no longer requires Serializable, so the casts in this line should be removed. As is, this line throws an error when it gets to the non-serializable Hibernate Enum class.


Since Emmanuel has patched Annotations, making the class Serializable, I'll hopefully be able to solve my problem by building the Annotations .jar from source out of Subversion. Don't suppose there are nightly snapshots sitting around anywhere, are there ...? :)

Thanks all for the help.
Rogers


gluck wrote:
Hi

ehcache-1.2 and the new provider do not require Serializable.

You can test this out before it gets merged into Hibernate 3.2 by downloading ehcache-1.2rc1 from http://ehcache.sf.net. A candidate Hibernate provider can be found here: http://ehcache.sourceforge.net/EhCache12Provider.java. Change your Hibernate configuration to use that provider.

Greg Luck


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 9:43 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I have just released, no nightly snamshot needed ;-)

_________________
Emmanuel


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