2013-02-07 75 views
1

我體驗了我目前使用hibernate 3.6.2-final和ehcache 2.6.2分析的錯誤。使用自定義休眠類型時ehcache錯誤?

我最近開始使用自定義類型來處理數據庫中存儲爲「OUI」/「NON」的值,作爲java中的布爾值。

我正在使用org.hibernate.type.YesNoType的改編。這部分工作。

當我打開的Ehcache上,我得到一個高速緩存命中以下錯誤:在對象上進行設置字符串值企圖被處理,因爲屬性是一個boolean值,失敗......

在我的日誌中,我有: 在類中的IllegalArgumentException:fr.senat.base_senateurs.libsen.hbm.Dpt,setter方法的屬性:evetempub 預期類型:java.lang.Boolean,實際值:java.lang.String

兩個有趣的例外是:

org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of fr.senat.base_senateurs.libsen.hbm.Dpt.evetempub 
org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:128) 
org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:583) 
org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:229) 
org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3847) 
org.hibernate.cache.entry.CacheEntry.assemble(CacheEntry.java:137) 
org.hibernate.cache.entry.CacheEntry.assemble(CacheEntry.java:104) 
org.hibernate.event.def.DefaultLoadEventListener.assembleCacheEntry(DefaultLoadEventListener.java:651) 
org.hibernate.event.def.DefaultLoadEventListener.loadFromSecondLevelCache(DefaultLoadEventListener.java:605) 
org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:459) 
org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227) 
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:147) 
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090) 
org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:1026) 
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:176) 
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215) 
org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190) 
fr.senat.base_senateurs.libsen.hbm.Dpt_$$_javassist_77.getDptlib(Dpt_$$_javassist_77.java) 
sun.reflect.GeneratedMethodAccessor106.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
java.lang.reflect.Method.invoke(Method.java:601) 
javax.el.BeanELResolver.getValue(BeanELResolver.java:87) 
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67) 
org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:179) 
org.apache.el.parser.AstValue.getValue(AstValue.java:183) 
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185) 
org.apache.webbeans.el.WrappedValueExpression.getValue(WrappedValueExpression.java:68) 
org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression.getValue(ContextAwareTagValueExpression.java:96) 
javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:249) 
javax.faces.component.UIOutput.getValue(UIOutput.java:67) 
org.apache.myfaces.shared.renderkit.RendererUtils.getValue(RendererUtils.java:491) 
org.apache.myfaces.shared.renderkit.RendererUtils.getStringValue(RendererUtils.java:334) 
org.apache.myfaces.shared.renderkit.html.HtmlTextRendererBase.renderOutput(HtmlTextRendererBase.java:92) 
org.apache.myfaces.shared.renderkit.html.HtmlTextRendererBase.encodeEnd(HtmlTextRendererBase.java:79) 
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:663) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:543) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:539) 
org.primefaces.component.datatable.DataTableRenderer.encodeRegularCell(DataTableRenderer.java:743) 
org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:695) 
org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:603) 
org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:220) 
org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:192) 
org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:74) 
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:663) 
javax.faces.component.UIData.encodeEnd(UIData.java:1699) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:543) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:539) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:539) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:539) 
org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1981) 
org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285) 
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59) 
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59) 
org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:116) 
org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:241) 
org.apache.myfaces.extensions.cdi.jsf2.impl.listener.phase.CodiLifecycleWrapper.render(CodiLifecycleWrapper.java:126) 
javax.faces.webapp.FacesServlet.service(FacesServlet.java:199) 
fr.senat.faces.filters.HibernateNoSessionRequestFilter.doFilter(HibernateNoSessionRequestFilter.java:44) 

java.lang.IllegalArgumentException: [email protected] 
sun.reflect.GeneratedMethodAccessor129.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
java.lang.reflect.Method.invoke(Method.java:601) 
org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:66) 
org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:583) 
org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:229) 
org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3847) 
org.hibernate.cache.entry.CacheEntry.assemble(CacheEntry.java:137) 
org.hibernate.cache.entry.CacheEntry.assemble(CacheEntry.java:104) 
org.hibernate.event.def.DefaultLoadEventListener.assembleCacheEntry(DefaultLoadEventListener.java:651) 
org.hibernate.event.def.DefaultLoadEventListener.loadFromSecondLevelCache(DefaultLoadEventListener.java:605) 
org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:459) 
org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227) 
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:147) 
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090) 
org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:1026) 
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:176) 
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215) 
org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190) 
fr.senat.base_senateurs.libsen.hbm.Dpt_$$_javassist_77.getDptlib(Dpt_$$_javassist_77.java) 
sun.reflect.GeneratedMethodAccessor106.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
java.lang.reflect.Method.invoke(Method.java:601) 
javax.el.BeanELResolver.getValue(BeanELResolver.java:87) 
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67) 
org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:179) 
org.apache.el.parser.AstValue.getValue(AstValue.java:183) 
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185) 
org.apache.webbeans.el.WrappedValueExpression.getValue(WrappedValueExpression.java:68) 
org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression.getValue(ContextAwareTagValueExpression.java:96) 
javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:249) 
javax.faces.component.UIOutput.getValue(UIOutput.java:67) 
org.apache.myfaces.shared.renderkit.RendererUtils.getValue(RendererUtils.java:491) 
org.apache.myfaces.shared.renderkit.RendererUtils.getStringValue(RendererUtils.java:334) 
org.apache.myfaces.shared.renderkit.html.HtmlTextRendererBase.renderOutput(HtmlTextRendererBase.java:92) 
org.apache.myfaces.shared.renderkit.html.HtmlTextRendererBase.encodeEnd(HtmlTextRendererBase.java:79) 
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:663) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:543) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:539) 
org.primefaces.component.datatable.DataTableRenderer.encodeRegularCell(DataTableRenderer.java:743) 
org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:695) 
org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:603) 
org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:220) 
org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:192) 
org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:74) 
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:663) 
javax.faces.component.UIData.encodeEnd(UIData.java:1699) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:543) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:539) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:539) 
javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:539) 
org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1981) 
org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285) 
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59) 
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59) 
org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:116) 
org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:241) 
org.apache.myfaces.extensions.cdi.jsf2.impl.listener.phase.CodiLifecycleWrapper.render(CodiLifecycleWrapper.java:126) 
javax.faces.webapp.FacesServlet.service(FacesServlet.java:199) 
fr.senat.faces.filters.HibernateNoSessionRequestFilter.doFilter(HibernateNoSessionRequestFilter.java:44) 

你有一個想法(除了停止使用二級緩存:-))?

回答

2

好的,所以,實際上,我的自定義類型很好的假設並不是那麼真實。 我知道這是冬眠的YesNo類型的複製粘貼,所以我很有信心... 但事實並非如此。

問題是它的組裝和拆卸方法不一致。

在拆機,值(此處爲布爾型)是由下面的片段轉化成字符串:

@Override 
public Serializable disassemble(Object value) { 
    return (value == null) ? null : value.toString(); 
} 

但是在組裝時,該字符串左「原樣」由下面的代碼片斷:

@Override 
public Object assemble(Serializable cached, Object owner) { 
    return (cached == null) ? null : cached; 
} 

正確的實現是:

@Override 
public Object assemble(Serializable cached, Object owner) { 
    if(cached == null) { 
     return null; 
    } 
    if(cached.equals("true")) { 
     return true; 
    } 
    return false; 
} 

由於組裝/拆卸我直到在緩存中執行了一些序列化之前,thod對才被使用,直到利用二級緩存的應用程序使用它時才檢測到該錯誤。

在從數據庫中的「常規」對象負載,AstractEntityPersister#水合物是由loadFromResultSet(org.hibernate.loader.Loader的線1527)稱爲 水合物通過調用nullSafeGet的類型

在執行自定義類型的對話緩存命中,緩存項由org.hibernate.cache.entry.StructuredCacheEntry#destructure「拆分」,然後由assembleCacheEntry進行彙編。在某些時候,它會調用自定義類型「assemble」方法。

請注意,爲了提高效率,我會盡量避免字符串比較,但您會明白... :-)