1

我遇到了我目前分析的 hibernate 3.6.2-final 和 ehcache 2.6.2 的错误。

我最近开始使用自定义类型来处理在数据库中存储为“OUI”/“NON”的值作为 java 中的布尔值。

我正在使用 org.hibernate.type.YesNoType 的改编版本。这部分有效。

当我打开 ehcache 时,我在缓存命中时遇到以下错误:尝试在正在处理的对象上设置字符串值,但由于属性是布尔值而失败...

在我的日志中,我有:类中的 IllegalArgumentException:fr.senat.base_senateurs.libsen.hbm.Dpt,属性的设置方法: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: java.lang.ClassCastException@4194c10f
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)

你有什么想法(除了停止使用二级缓存:-))?

4

1 回答 1

2

好吧,事实上,我的自定义类型很好的假设并不是那么正确。我知道这有点像 hibernate 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;
}

由于直到在缓存中执行了一些序列化后才使用组装/反汇编方法对,因此直到利用二级缓存的应用程序使用它时才检测到错误。

在从数据库加载“常规”对象时,AstractEntityPersister#hydrate 由 loadFromResultSet(org.hibernate.loader.Loader 的第 1527 行)调用,水合物通过在类型上调用 nullSafeGet 来执行自定义类型对话

在缓存命中时,缓存条目由 org.hibernate.cache.entry.StructuredCacheEntry#destructure “拆分”,然后由 assembleCacheEntry 组装。在某些时候,它会调用自定义类型的“assemble”方法。

请注意,为了提高效率,我会尽量避免字符串比较,但你明白了...... :-)

于 2013-02-07T15:01:57.807 回答