0
我正在開發一個使用JSF2.0/Primefaces,EJB3.1,Hibernate的項目。 我創建了一個企業應用程序,使用CDI將EJB3 Facades注入到JSF Managed Beans中,並創建了調用託管bean的網頁。當我運行項目時,我可以列出數據行並查看行詳細信息,但是當我從數據表中選擇一行並單擊編輯或刪除按鈕時,會出現問題。EJB3.1 JSF2.0 CDI ejbRef轉換
SEVERE: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.procc.jpa.Alert.responseCollection, could not initialize proxy - no Session
SEVERE: java.lang.IllegalStateException: Unable to convert ejbRef for ejb AlertFacade to a business object of type interface com.procc.dao.AlertFacadeLocal
at com.sun.ejb.containers.EjbContainerServicesImpl.getBusinessObject(EjbContainerServicesImpl.java:114)
at org.glassfish.weld.ejb.SessionObjectReferenceImpl.getBusinessObject(SessionObjectReferenceImpl.java:63)
at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:108)
at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:105)
at com.procc.dao.AlertFacade$Proxy$_$$_Weld$Proxy$.remove(AlertFacade$Proxy$_$$_Weld$Proxy$.java)
at com.procc.backing.AlertController.persist(AlertController.java:118)
at com.procc.backing.AlertController.delete(AlertController.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIData.broadcast(UIData.java:1093)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
這是代碼片段:
@ManagedBean(name = "alertController")
@SessionScoped
public class AlertController extends AbstractController<Alert> implements Serializable {
@Inject private AlertFacadeLocal ejbFacade;
private Class<Alert> itemClass;
private Alert selected;
private List<Alert> items;
private enum PersistAction {
CREATE,
DELETE,
UPDATE
}
public AlertController(){
itemClass = Alert.class;
}
public void delete(ActionEvent event) {
String msg = ResourceBundle.getBundle("/Bundle").getString(itemClass.getSimpleName() + "Deleted");
persist(AlertController.PersistAction.DELETE, msg);
if (!isValidationFailed()) {
selected = null; // Remove selection
items = null; // Invalidate list of items to trigger re-query.
}
}
private void persist(AlertController.PersistAction persistAction, String successMessage) {
if (selected != null) {
this.setEmbeddableKeys();
try {
if (persistAction != AlertController.PersistAction.DELETE) {
this.ejbFacade.edit(selected);
} else {
this.ejbFacade.remove(selected);
}
JsfUtil.addSuccessMessage(successMessage);
} catch (EJBException ex) {
String msg = "";
Throwable cause = JsfUtil.getRootCause(ex.getCause());
if (cause != null) {
msg = cause.getLocalizedMessage();
}
if (msg.length() > 0) {
JsfUtil.addErrorMessage(msg);
} else {
JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
}
} catch (Exception ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
}
}
}
在JSF頁面,我得到:
<p:panel header="#{bundle.ListAlertTitle}" style="border:0px;">
<p:dataTable id="datalist" value="#{alertController.items}" var="item"
selectionMode="single" selection="#{alertController.selected}"
rowKey="#{item.alertId}"
paginator="true"
rows="10"
rowsPerPageTemplate="10,20,30"
>
<p:ajax event="rowSelect" update="viewButton editButton deleteButton"/>
<p:ajax event="rowUnselect" update="viewButton editButton deleteButton"/>
<p:column sortBy="#{item.alertId}" filterBy="#{item.alertId}">
<f:facet name="header">
<h:outputText value="#{bundle.ListAlertTitle_alertId}"/>
</f:facet>
<h:outputText value="#{item.alertId}"/>
</p:column>
....
<p:commandButton id="viewButton" icon="ui-icon-search" value="#{bundle.View}" update=":AlertViewForm" oncomplete="AlertViewDialog.show()" disabled="#{empty alertController.selected}"/>
<p:commandButton id="editButton" icon="ui-icon-pencil" value="#{bundle.Edit}" update=":AlertEditForm" oncomplete="AlertEditDialog.show()" disabled="#{empty alertController.selected}"/>
<p:commandButton id="deleteButton" icon="ui-icon-trash" value="#{bundle.Delete}" actionListener="#{alertController.delete}" update=":growl,datalist" disabled="#{empty alertController.selected}"/>
如果還有其他問題,請點擊[問問題](http://stackoverflow.com/questions/ask)按鈕。謝謝。 – Pang