![](/img/trans.png)
[英]javax.el.ELException: Error reading [name] on type Person
[英]javax.el.ELException: Cannot convert Protocol@7ebc9002 of type class Protocol to class Protocol$$EnhancerByCGLIB$$22af7fa3 (lazy loadding)
我很難使休眠3.1延遲加載與JSF 1.2一起使用
Caused by: javax.el.ELException: Cannot convert foo.bar.Protocol@7ebc9002 of type class foo.bar.Protocol to class foo.bar.Protocol$$EnhancerByCGLIB$$22af7fa3
at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:438)
at org.apache.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:46)
at com.sun.faces.renderkit.html_basic.RadioRenderer.renderOption(RadioRenderer.java:87)
at com.sun.faces.renderkit.html_basic.SelectManyCheckboxListRenderer.encodeEnd(SelectManyCheckboxListRenderer.java:146)
我讀到休眠將按需替換延遲加載代理,但似乎不適用於JSF轉換器調用。
請注意,協議綁定到視圖中的單選按鈕
您知道如何解決此問題嗎? 我找不到與我有相同問題的人。
applicationContext:
<bean id="protocol" class="foo.bar.Protocol" abstract="false"
lazy-init="default" autowire="byName" dependency-check="default" scope="session">
<aop:scoped-proxy />
</bean>
<bean id="protocolConverter" class="foo.bar.ProtocolConverter" abstract="false"
lazy-init="default" autowire="byName" dependency-check="default" scope="singleton">
<property name="protocolDAO" ref="protocolDAO" />
</bean>
查看:
<h:selectOneRadio value="#{pingControler.ping.protocol}" converter="#{protocolConverter}">
<f:selectItems value="#{pingControler.allProtocolsSelectItems}" />
<a4j:support event="onchange" reRender="foo1,foo2" />
</h:selectOneRadio>
ping:
public class Ping {
// Fields
private Integer pingId;
private Protocol protocol;
...
}
pingControler:
private Ping ping;
public void init(ActionEvent event) {
ping = new Ping();
}
public void save(ActionEvent event) throws Exception {
if (ping.getPingId() == null) {
pingPersistent.addPing(ping);
} else {
pingPersistent.updatePing(ping);
}
}
我(最終)想出了解決方法。 我不確定這是否正確,但是可以。
我添加了以下代碼:
private static <T> T initializeAndUnproxy(T entity) {
if (entity == null) {
throw new NullPointerException("Entity passed for initialization is null");
}
Hibernate.initialize(entity);
if (entity instanceof HibernateProxy) {
entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
}
return entity;
}
我在轉換器中這樣稱呼它:
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
Protocol protocol = protocolDAO.findById(new Integer(value));
return initializeAndUnproxy(protocol);
}
代替這個:
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
return protocolDAO.findById(new Integer(value));
}
順便說一句,當我說$$ EnhancedByCGLIB $$必然意味着一個休眠代理時,我犯了一個錯誤。 我在其他地方讀到它是一個庫,例如,Spring依賴注入也可以使用它。 只是讓你知道。
我希望這可以幫助剩下的幾個jsf 1.2 / hibernate 3.1用戶。 我應該自我評價嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.