@ViewScoped @ManagedBean 中的 @EJB 导致 java.io.NotSerializedException

发布于 2024-12-09 21:40:13 字数 17710 浏览 0 评论 0原文

我读过 @ViewScoped 托管 bean 中的 @EJB 导致 java.lang.Error 。 io.NotSerializedException,但我的状态保存设置是server

这是我所拥有的:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>sispra</display-name>
    <welcome-file-list>
        <welcome-file>index.jsf</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <context-param>
        <param-name>facelets.BUILD_BEFORE_RESTORE</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>glass-x</param-value>
    </context-param>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Secure Application</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>user</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>fileRealm</realm-name>
    </login-config>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <security-role>
        <role-name>user</role-name>
    </security-role>
</web-app>

customer.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">      

    <h:head>
        <title>TODO supply a title</title>
    </h:head>

    <h:body>

        <p:growl id="messages"/>

        <h:form>

            <p:commandButton actionListener="#{customerController.create}" value="save" update="@form :messages"/>

            <p:panel>
                <f:facet name="header">
                    <h:outputText value="Details"/>
                </f:facet>

                <h:panelGrid columns="3">

                    <h:outputLabel for="name" value="#{bundle['person.name']}"/>
                    <p:inputText id="name" label="#{bundle['person.name']}" value="#{customerController.selected.name}"/>
                    <p:message for="name"/>

                    <h:outputLabel for="surname" value="#{bundle['person.surname']}"/>
                    <p:inputText id="surname" label="#{bundle['person.surname']}" value="#{customerController.selected.surname}"/>
                    <p:message for="surname"/>

                </h:panelGrid>
            </p:panel>

        </h:form>

    </h:body>
</html>

CustomerController.java:

package it.shape.sispra.controllers;

import it.shape.sispra.ejb.PersonFacade;
import it.shape.sispra.entities.Customer;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class CustomerController extends AbstractController<Customer>
{
    private static final long serialVersionUID = 134755304347034L;

    @EJB
    private PersonFacade facade;

    public CustomerController()
    {
        super(Customer.class);
    }

    @Override
    public PersonFacade getFacade()
    {
        return facade;
    }
}

PersonFacade.java:

package it.shape.sispra.ejb;

import it.shape.sispra.entities.Person;
import javax.ejb.Stateless;

@Stateless
public class PersonFacade extends AbstractFacade<Person>
{
    private static final long serialVersionUID = 4357823648345L;

    public PersonFacade()
    {
        super(Person.class);
    }

}

AbstractFacade.java:

package it.shape.sispra.ejb;

import it.shape.sispra.entities.AbstractEntity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.eclipse.persistence.jpa.JpaHelper;
import org.eclipse.persistence.queries.QueryByExamplePolicy;
import org.eclipse.persistence.queries.ReadAllQuery;
import org.eclipse.persistence.queries.ReadObjectQuery;

public abstract class AbstractFacade<T extends AbstractEntity> implements Serializable
{
    private static final long serialVersionUID = 12467890452346123L;

    @PersistenceContext(unitName = "sispra")
    private EntityManager em;

    private final Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass)
    {
        this.entityClass = entityClass;
    }

    public EntityManager getEntityManager()
    {
        return em;
    }

    public void create(T entity)
    {
        getEntityManager().persist(entity);
    }

    public void edit(T entity)
    {
        getEntityManager().merge(entity);
    }

    public void remove(T entity)
    {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public void refresh(T entity)
    {
        getEntityManager().refresh(entity);
    }

    public T find(Object id)
    {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll()
    {
        CriteriaQuery<T> cq = getEntityManager().getCriteriaBuilder().createQuery(entityClass);
        cq.select(cq.from(entityClass));

        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int first, int max)
    {
        CriteriaQuery<T> cq = getEntityManager().getCriteriaBuilder().createQuery(entityClass);
        cq.select(cq.from(entityClass));

        TypedQuery<T> q = getEntityManager().createQuery(cq);
        q.setMaxResults(max);
        q.setFirstResult(first);

        return q.getResultList();
    }

    public int count()
    {
        CriteriaQuery<Long> cq = getEntityManager().getCriteriaBuilder().createQuery(Long.class);
        Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        TypedQuery<Long> q = getEntityManager().createQuery(cq);
        return q.getSingleResult().intValue();
    }

    @SuppressWarnings("unchecked")
    public T findByExample(T entity)
    {
        // Create a native EclipseLink query using QBE policy
        QueryByExamplePolicy policy = new QueryByExamplePolicy();
        policy.addSpecialOperation(String.class, "like");

        ReadObjectQuery roq = new ReadObjectQuery(entity, policy);

        // Wrap the native query in a standard JPA Query and execute it
        Query query = JpaHelper.createQuery(roq, getEntityManager());

        return (T) query.getSingleResult();
    }

    @SuppressWarnings("unchecked")
    public List<T> findAllByExample(T entity)
    {
        // Create a native EclipseLink query using QBE policy
        QueryByExamplePolicy policy = new QueryByExamplePolicy();
        policy.addSpecialOperation(String.class, "like");

        ReadAllQuery raq = new ReadAllQuery(entity, policy);

        // Wrap the native query in a standard JPA Query and execute it
        Query query = JpaHelper.createQuery(raq, getEntityManager());

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<T> findByExample(T entity, int start, int max)
    {
        // Create a native EclipseLink query using QBE policy
        QueryByExamplePolicy policy = new QueryByExamplePolicy();
        policy.addSpecialOperation(String.class, "like");

        ReadAllQuery raq = new ReadAllQuery(entity, policy);

        // Wrap the native query in a standard JPA Query and execute it
        Query query = JpaHelper.createQuery(raq, getEntityManager());
        query.setFirstResult(start);
        query.setMaxResults(max);

        return query.getResultList();
    }

    public int countByExample(T entity)
    {
        //TODO find a better way...
        return findAllByExample(entity).size();
    }
}

这是堆栈跟踪:

GRAVE: Exiting serializeView - Could not serialize state: com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate
java.io.NotSerializableException: com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
  at java.util.HashMap.writeObject(HashMap.java:1001)
  at sun.reflect.GeneratedMethodAccessor45.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
  at java.util.HashMap.writeObject(HashMap.java:1001)
  at sun.reflect.GeneratedMethodAccessor45.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
  at org.apache.myfaces.renderkit.ServerSideStateCacheImpl.serializeView(ServerSideStateCacheImpl.java:357)
  at org.apache.myfaces.renderkit.ServerSideStateCacheImpl.saveSerializedViewInServletSession(ServerSideStateCacheImpl.java:220)
  at org.apache.myfaces.renderkit.ServerSideStateCacheImpl.saveSerializedView(ServerSideStateCacheImpl.java:798)
  at org.apache.myfaces.renderkit.html.HtmlResponseStateManager.saveState(HtmlResponseStateManager.java:127)
  at org.apache.myfaces.application.StateManagerImpl.saveView(StateManagerImpl.java:166)
  at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1554)
  at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:281)
  at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
  at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
  at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
  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 com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
  at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
  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:662)

我知道我可以通过将 PersonFacade 声明为 transient 并在重建后使用 JNDI 获取 EJB 引用来解决此问题,但我真的不喜欢这种方法。

Glassfish 3.1.1 是否有可能提供不可序列化的 EJB?有没有办法一起使用@EJB@ViewScoped

更新: 我发现这是一个与 MyFaces 相关的问题,使用 mojarra 一切正常

I've read @EJB in @ViewScoped managed bean causes java.io.NotSerializableException, but my state saving setting is server.

Here is what I have:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>sispra</display-name>
    <welcome-file-list>
        <welcome-file>index.jsf</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <context-param>
        <param-name>facelets.BUILD_BEFORE_RESTORE</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>glass-x</param-value>
    </context-param>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Secure Application</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>user</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>fileRealm</realm-name>
    </login-config>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <security-role>
        <role-name>user</role-name>
    </security-role>
</web-app>

customer.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">      

    <h:head>
        <title>TODO supply a title</title>
    </h:head>

    <h:body>

        <p:growl id="messages"/>

        <h:form>

            <p:commandButton actionListener="#{customerController.create}" value="save" update="@form :messages"/>

            <p:panel>
                <f:facet name="header">
                    <h:outputText value="Details"/>
                </f:facet>

                <h:panelGrid columns="3">

                    <h:outputLabel for="name" value="#{bundle['person.name']}"/>
                    <p:inputText id="name" label="#{bundle['person.name']}" value="#{customerController.selected.name}"/>
                    <p:message for="name"/>

                    <h:outputLabel for="surname" value="#{bundle['person.surname']}"/>
                    <p:inputText id="surname" label="#{bundle['person.surname']}" value="#{customerController.selected.surname}"/>
                    <p:message for="surname"/>

                </h:panelGrid>
            </p:panel>

        </h:form>

    </h:body>
</html>

CustomerController.java:

package it.shape.sispra.controllers;

import it.shape.sispra.ejb.PersonFacade;
import it.shape.sispra.entities.Customer;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class CustomerController extends AbstractController<Customer>
{
    private static final long serialVersionUID = 134755304347034L;

    @EJB
    private PersonFacade facade;

    public CustomerController()
    {
        super(Customer.class);
    }

    @Override
    public PersonFacade getFacade()
    {
        return facade;
    }
}

PersonFacade.java:

package it.shape.sispra.ejb;

import it.shape.sispra.entities.Person;
import javax.ejb.Stateless;

@Stateless
public class PersonFacade extends AbstractFacade<Person>
{
    private static final long serialVersionUID = 4357823648345L;

    public PersonFacade()
    {
        super(Person.class);
    }

}

AbstractFacade.java:

package it.shape.sispra.ejb;

import it.shape.sispra.entities.AbstractEntity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.eclipse.persistence.jpa.JpaHelper;
import org.eclipse.persistence.queries.QueryByExamplePolicy;
import org.eclipse.persistence.queries.ReadAllQuery;
import org.eclipse.persistence.queries.ReadObjectQuery;

public abstract class AbstractFacade<T extends AbstractEntity> implements Serializable
{
    private static final long serialVersionUID = 12467890452346123L;

    @PersistenceContext(unitName = "sispra")
    private EntityManager em;

    private final Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass)
    {
        this.entityClass = entityClass;
    }

    public EntityManager getEntityManager()
    {
        return em;
    }

    public void create(T entity)
    {
        getEntityManager().persist(entity);
    }

    public void edit(T entity)
    {
        getEntityManager().merge(entity);
    }

    public void remove(T entity)
    {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public void refresh(T entity)
    {
        getEntityManager().refresh(entity);
    }

    public T find(Object id)
    {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll()
    {
        CriteriaQuery<T> cq = getEntityManager().getCriteriaBuilder().createQuery(entityClass);
        cq.select(cq.from(entityClass));

        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int first, int max)
    {
        CriteriaQuery<T> cq = getEntityManager().getCriteriaBuilder().createQuery(entityClass);
        cq.select(cq.from(entityClass));

        TypedQuery<T> q = getEntityManager().createQuery(cq);
        q.setMaxResults(max);
        q.setFirstResult(first);

        return q.getResultList();
    }

    public int count()
    {
        CriteriaQuery<Long> cq = getEntityManager().getCriteriaBuilder().createQuery(Long.class);
        Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        TypedQuery<Long> q = getEntityManager().createQuery(cq);
        return q.getSingleResult().intValue();
    }

    @SuppressWarnings("unchecked")
    public T findByExample(T entity)
    {
        // Create a native EclipseLink query using QBE policy
        QueryByExamplePolicy policy = new QueryByExamplePolicy();
        policy.addSpecialOperation(String.class, "like");

        ReadObjectQuery roq = new ReadObjectQuery(entity, policy);

        // Wrap the native query in a standard JPA Query and execute it
        Query query = JpaHelper.createQuery(roq, getEntityManager());

        return (T) query.getSingleResult();
    }

    @SuppressWarnings("unchecked")
    public List<T> findAllByExample(T entity)
    {
        // Create a native EclipseLink query using QBE policy
        QueryByExamplePolicy policy = new QueryByExamplePolicy();
        policy.addSpecialOperation(String.class, "like");

        ReadAllQuery raq = new ReadAllQuery(entity, policy);

        // Wrap the native query in a standard JPA Query and execute it
        Query query = JpaHelper.createQuery(raq, getEntityManager());

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<T> findByExample(T entity, int start, int max)
    {
        // Create a native EclipseLink query using QBE policy
        QueryByExamplePolicy policy = new QueryByExamplePolicy();
        policy.addSpecialOperation(String.class, "like");

        ReadAllQuery raq = new ReadAllQuery(entity, policy);

        // Wrap the native query in a standard JPA Query and execute it
        Query query = JpaHelper.createQuery(raq, getEntityManager());
        query.setFirstResult(start);
        query.setMaxResults(max);

        return query.getResultList();
    }

    public int countByExample(T entity)
    {
        //TODO find a better way...
        return findAllByExample(entity).size();
    }
}

and this is the stacktrace:

GRAVE: Exiting serializeView - Could not serialize state: com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate
java.io.NotSerializableException: com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
  at java.util.HashMap.writeObject(HashMap.java:1001)
  at sun.reflect.GeneratedMethodAccessor45.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
  at java.util.HashMap.writeObject(HashMap.java:1001)
  at sun.reflect.GeneratedMethodAccessor45.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
  at org.apache.myfaces.renderkit.ServerSideStateCacheImpl.serializeView(ServerSideStateCacheImpl.java:357)
  at org.apache.myfaces.renderkit.ServerSideStateCacheImpl.saveSerializedViewInServletSession(ServerSideStateCacheImpl.java:220)
  at org.apache.myfaces.renderkit.ServerSideStateCacheImpl.saveSerializedView(ServerSideStateCacheImpl.java:798)
  at org.apache.myfaces.renderkit.html.HtmlResponseStateManager.saveState(HtmlResponseStateManager.java:127)
  at org.apache.myfaces.application.StateManagerImpl.saveView(StateManagerImpl.java:166)
  at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1554)
  at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:281)
  at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
  at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
  at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
  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 com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
  at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
  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:662)

I know I can workaround this by declaring PersonFacade as transient and use JNDI to get EJB reference after recontruction, but I really dislike this approach.

Is it possible that Glassfish 3.1.1 provides non-serializable EJBs? Is there a way to use @EJB and @ViewScoped together?

update:
i found that this is a MyFaces related issue, all is working fine using mojarra

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏见 2024-12-16 21:40:14

我也看到了这个问题。我过去所做的是将我的 bean 拆分为 StateBean (即 @ViewScoped)和 ActionsBean (即 @RequestScoped<) /代码>。 ActionBeanStateBean 以及任何 EJB 或不可序列化资源访问对象一起注入。在前端,您使用 StateBean 来访问属性,使用 ActionsBean 来执行操作。

我很想听听其他人的意见,他们会让我的“豆子分裂”模式被弃用。

这是我所做的一个示例:

@ManagedBean
@ViewScoped
public class CustomControllerStateBean implements Serializable {

  private static final long serialVersionUID = 134755304347034L;

  private Person selected;

  public Person getSelected() {
    return selected;
  }

  public void setSelected(Person selected) {
    this.selected = selected;
  }
}

注意 CustomControllerStateBean@ViewScoped 并且是 Serialized 且仅包含 Serialized code> 对象。

@ManagedBean
@RequestScoped
public class CustomControllerActionsBean {

  @EJB
  private PersonFacade facade;
  @Inject
  private CustomControllerStateBean state;

  public void create() {
    facade.create(state.getSelected());
  }
}

注意 CustomControllerActionsBean@RequestScoped 并且不是 Serialized 包含非可序列化对象。

您的前端不会像这样:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">      

    <h:head>
        <title>TODO supply a title</title>
    </h:head>

    <h:body>

        <p:growl id="messages"/>

        <h:form>

            <p:commandButton actionListener="#{customerControllerActionsBean.create}" value="save" update="@form :messages"/>

            <p:panel>
                <f:facet name="header">
                    <h:outputText value="Details"/>
                </f:facet>

                <h:panelGrid columns="3">

                    <h:outputLabel for="name" value="#{bundle['person.name']}"/>
                    <p:inputText id="name" label="#{bundle['person.name']}" value="#{customerControllerStateBean.selected.name}"/>
                    <p:message for="name"/>

                    <h:outputLabel for="surname" value="#{bundle['person.surname']}"/>
                    <p:inputText id="surname" label="#{bundle['person.surname']}" value="#{customerControllerStateBean.selected.surname}"/>
                    <p:message for="surname"/>

                </h:panelGrid>
            </p:panel>

        </h:form>

    </h:body>
</html>

注意CustomControllerActionsBeanp的顶部使用: commandButtonCustomControllerStateBean 用于 p:inputText 中。

I have seen the problem too. What I have done in the past is split my bean into a StateBean which is @ViewScoped and an ActionsBean which is @RequestScoped. The ActionBean is injected with the StateBean as well as any EJBs or non-serializable resource accessing objects. On the front-end you use the StateBean for accessing properties and ActionsBean for performing actions.

I would love to hear from someone else who would make my "bean splitting" pattern deprecated.

This is an example of what I do:

@ManagedBean
@ViewScoped
public class CustomControllerStateBean implements Serializable {

  private static final long serialVersionUID = 134755304347034L;

  private Person selected;

  public Person getSelected() {
    return selected;
  }

  public void setSelected(Person selected) {
    this.selected = selected;
  }
}

Notice CustomControllerStateBean is @ViewScoped and is Serializable an only contains Serializable objects.

@ManagedBean
@RequestScoped
public class CustomControllerActionsBean {

  @EJB
  private PersonFacade facade;
  @Inject
  private CustomControllerStateBean state;

  public void create() {
    facade.create(state.getSelected());
  }
}

Notice CustomControllerActionsBean is @RequestScoped and is NOT Serializable an contains non-Serializable objects.

Your front-end will no look like this:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">      

    <h:head>
        <title>TODO supply a title</title>
    </h:head>

    <h:body>

        <p:growl id="messages"/>

        <h:form>

            <p:commandButton actionListener="#{customerControllerActionsBean.create}" value="save" update="@form :messages"/>

            <p:panel>
                <f:facet name="header">
                    <h:outputText value="Details"/>
                </f:facet>

                <h:panelGrid columns="3">

                    <h:outputLabel for="name" value="#{bundle['person.name']}"/>
                    <p:inputText id="name" label="#{bundle['person.name']}" value="#{customerControllerStateBean.selected.name}"/>
                    <p:message for="name"/>

                    <h:outputLabel for="surname" value="#{bundle['person.surname']}"/>
                    <p:inputText id="surname" label="#{bundle['person.surname']}" value="#{customerControllerStateBean.selected.surname}"/>
                    <p:message for="surname"/>

                </h:panelGrid>
            </p:panel>

        </h:form>

    </h:body>
</html>

Notice CustomControllerActionsBean is used at the top in the p:commandButton and CustomControllerStateBean is used in the p:inputTexts.

幻梦 2024-12-16 21:40:14

:) 我遇到了同样的问题: https://issues.apache.org/jira/browse /MYFACES-3581

Apache TomEE 的 David Blevins 帮助我记录了 MyFaces 的错误和补丁。问题是生成的代理类不在其反序列化器的类加载器路径上。

解决方法是将此 Web 应用程序参数设置为 false:org.apache.myfaces.SERIALIZE_STATE_IN_SESSION

:) I had the same problem: https://issues.apache.org/jira/browse/MYFACES-3581

David Blevins over at the Apache TomEE helped me a log a bug and patch for MyFaces. The problem was generated proxy classes aren't on the classloader path for their deserializer.

A workaround is to set this web-app param to false: org.apache.myfaces.SERIALIZE_STATE_IN_SESSION

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文