Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java bean属性上的SEAM JSF Lazy Init异常_Java_Jsf_Richfaces_Seam_Seam Conversation - Fatal编程技术网

Java bean属性上的SEAM JSF Lazy Init异常

Java bean属性上的SEAM JSF Lazy Init异常,java,jsf,richfaces,seam,seam-conversation,Java,Jsf,Richfaces,Seam,Seam Conversation,我在SEAM(2.2.2)应用程序中遇到了一个延迟初始化异常问题,我使用的是SEAM管理的持久性上下文(与JPA),如本文所述 SEAM文档参考 在我的GenericDAO类中使用@In注入实体管理器 场景: 2012-12-24 15:30:34,661 SEVERE [facelets.viewhandler] (http-0.0.0.0-8080-3) Error Rendering View[/user/settings.xhtml]: javax.el.ELException: /us

我在SEAM(2.2.2)应用程序中遇到了一个延迟初始化异常问题,我使用的是SEAM管理的持久性上下文(与JPA),如本文所述

SEAM文档参考

在我的GenericDAO类中使用@In注入实体管理器

场景:

2012-12-24 15:30:34,661 SEVERE [facelets.viewhandler] (http-0.0.0.0-8080-3) Error Rendering View[/user/settings.xhtml]: javax.el.ELException: /user/settings.xhtml: org.hibernate.LazyInitializationException: could not initialize proxy - no Session

at com.webapp.entities.Client_$$_javassist_29.getLogoUrl
我有一个会话范围的bean,它注入了当前登录的用户实体(会话范围),当我试图通过页面中的JSF(el)惰性地加载一些额外的用户属性时,似乎抛出了谎言

堆栈跟踪错误:

2012-12-24 15:30:34,661 SEVERE [facelets.viewhandler] (http-0.0.0.0-8080-3) Error Rendering View[/user/settings.xhtml]: javax.el.ELException: /user/settings.xhtml: org.hibernate.LazyInitializationException: could not initialize proxy - no Session

at com.webapp.entities.Client_$$_javassist_29.getLogoUrl
起初我以为对话可能超时了,但这是通过注销用户来处理的,而不是撒谎

所以现在我想,可能是因为用户实体是从会话范围注入的,而动作bean是会话范围,所以对象以某种方式与实体管理器分离

不幸的是,不是每次都会抛出异常,所以我无法轻松地复制它(应用程序是活动的,所以我会在何时通过as获得错误)

我知道我可以通过设置用户属性来解决这个问题,但是我想先了解一下这个问题,并且不希望预先加载所有的实体

有关我的设置的更多详细信息:

2012-12-24 15:30:34,661 SEVERE [facelets.viewhandler] (http-0.0.0.0-8080-3) Error Rendering View[/user/settings.xhtml]: javax.el.ELException: /user/settings.xhtml: org.hibernate.LazyInitializationException: could not initialize proxy - no Session

at com.webapp.entities.Client_$$_javassist_29.getLogoUrl
components.xml:

<persistence:managed-persistence-context name="entityManager"
    auto-create="true"
    persistence-unit-jndi-name="java:/EntityManagerFactories/appData">  
</persistence:managed-persistence-context>

persistence.xml

<persistence-unit name="AppDatabase">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/appDatasource</jta-data-source>
    <properties>
        <property name="hibernate.connection.datasource" value="java:/appDatasource"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactories/appData"/>
        <property name="hibernate.cache.use_second_level_cache" value="false"/>
        <property name="hibernate.show_sql" value="false"/> 
    </properties>
</persistence-unit>

org.hibernate.ejb.HibernatePersistence
java:/appDatasource
通用刀

public abstract class GenericDAOBean<T>
    implements GenericDAO<T>, Serializable{

private static final long serialVersionUID = 1L;

private Class<T> entityBeanType;

@In private EntityManager em;

@SuppressWarnings("unchecked")
public GenericDAOBean() {
    this.entityBeanType = (Class<T>) ((ParameterizedType) getClass()
                            .getGenericSuperclass()).getActualTypeArguments()[0];
}

/**
 * Set the entity manager to use
 * 
 * @param em
 */
public void setEntityManager(EntityManager em) {
    this.em = em;
}

/**
 * Get the current seam entity manager
 * 
 * @return
 */
 protected EntityManager getEntityManager() {
 //Seam entity manager set this way as of version 2.2.0 
 //can't handle abstract classes and @In doesn't inject
 //into this as a parent class
 EntityManager entityManager = (EntityManager)Component.getInstance("entityManager");
 if (entityManager == null)
     throw new IllegalStateException("Seam EntityManager has not been set on "
     +getEntityBeanType().getClass().getName()+"DAO before usage!");
     return entityManager;
 }

//Further Generic method follow here which are removed for brevity
}
公共抽象类GenericDAOBean
实现GenericDAO,可序列化{
私有静态最终长serialVersionUID=1L;
私有类实体类型;
@私人实体经理em;
@抑制警告(“未选中”)
公共GenericDAOBean(){
this.entityBeanType=(类)((ParameteredType)getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
/**
*设置要使用的实体管理器
* 
*@param-em
*/
公共无效设置EntityManager(EntityManager em){
this.em=em;
}
/**
*获取当前seam实体管理器
* 
*@返回
*/
受保护的EntityManager getEntityManager(){
//自版本2.2.0起,Seam实体管理器以这种方式进行设置
//无法处理抽象类,@In不注入
//将其作为父类导入
EntityManager EntityManager=(EntityManager)组件.getInstance(“EntityManager”);
if(entityManager==null)
抛出新的IllegalStateException(“Seam EntityManager尚未设置为打开”
+getEntityBeanType().getClass().getName()+“使用前的DAO!”);
返回实体管理器;
}
//以下是为简洁起见删除的其他通用方法
}

如果您从ActionBean获取用户实体,请在ActionBean中尝试

User=entityManger.find(User.class,User.getId())

这应该将其加载到扩展持久性上下文中,因为它看起来像是分离的

在评论后编辑

你当然不会在getter方法中这样做,只要在你访问bean之前的任何方法中这样做一次,它就会被存储到你的扩展PC上,我会说在你动作bean的@Create方法中这样做。关于你的最后一个问题:它是否曾经被加载到Seam管理的持久性上下文中,而会话是绑定的


此外,您还应该在实体管理器中注入“@In-EntityManager-EntityManager”,因为这是您在application.xml中对托管持久性上下文的配置。如果您的实体曾经在本次对话中加载到EPC中,那么这就足够了。

但是当我通过JSF(el)访问用户实体时,我必须将.find()方法添加到Getter中,这意味着每次在页面中引用该实体时,我都会重新加载该实体!我希望首先找出实体分离的原因感谢更新,我将尝试添加到@Create方法,正如您所建议的,我已经在我的GenericDAO抽象类中使用了“@in EntityManager em”(参见原始帖子),或者您是否建议也将其注入ActionBean?您的配置是'name=“entityManager”或name=“em”?要保持一致,可以在需要的地方注入实体管理器。我建议在只调用一次的带有'@Create'标记的方法中,将您的用户而不是entityManager引入此对话的扩展持久性上下文。您可以通过'user=entityManger.find(user.class,user.getId())来实现。这是一个对象的延迟加载问题,该对象未连接到运行对话的扩展持久性上下文,并且该对象是用户。@DaveB您是否尝试过使用@Factory?如果使用该注释,则不会重新加载它,除非它为null。