GVKun编程网logo

Java如何修复org.hibernate.LazyInitializationException-无法初始化代理-没有会话

3

如果您想了解Java如何修复org.hibernate.LazyInitializationException-无法初始化代理-没有会话的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于c#

如果您想了解Java如何修复org.hibernate.LazyInitializationException-无法初始化代理-没有会话的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于c# – NHibernate.LazyInitializationException、Hibernate org.hibernate.LazyInitializationException:无法延迟初始化角色集合:、Hibernate-避免LazyInitializationException-从代理和会话中分离对象、Hibernate中的LazyInitializationException:无法初始化代理-没有会话的有价值的信息。

本文目录一览:

Java如何修复org.hibernate.LazyInitializationException-无法初始化代理-没有会话

Java如何修复org.hibernate.LazyInitializationException-无法初始化代理-没有会话

我得到以下异常:

Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)    at sei.persistence.wf.entities.Element_$$_jvstc68_47.getNote(Element_$$_jvstc68_47.java)    at JSON_to_XML.createBpmnRepresantation(JSON_to_XML.java:139)    at JSON_to_XML.main(JSON_to_XML.java:84)

当我尝试从以下几行拨打电话时:

Model subProcessModel = getModelByModelGroup(1112);System.out.println(subProcessModel.getElement().getNote());

getModelByModelGroup(int modelgroupid)首先实现了这样的方法:

public static Model getModelByModelGroup(int modelGroupId, boolean openTransaction) {    Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();         Transaction tx = null;    if (openTransaction) {        tx = session.getTransaction();    }    String responseMessage = "";    try {        if (openTransaction) {            tx.begin();        }        Query query = session.createQuery("from Model where modelGroup.id = :modelGroupId");        query.setParameter("modelGroupId", modelGroupId);        List<Model> modelList = (List<Model>)query.list();         Model model = null;        for (Model m : modelList) {            if (m.getModelType().getId() == 3) {                model = m;                break;            }        }        if (model == null) {            Object[] arrModels = modelList.toArray();            if (arrModels.length == 0) {                throw new Exception("Non esiste ");            }            model = (Model)arrModels[0];        }        if (openTransaction) {            tx.commit();        }        return model;   } catch(Exception ex) {       if (openTransaction) {           tx.rollback();       }       ex.printStackTrace();       if (responseMessage.compareTo("") == 0) {           responseMessage = "Error" + ex.getMessage();       }       return null;    }}

并得到了例外。然后一位朋友建议我始终测试该会话并获取当前会话,以避免出现此错误。所以我这样做:

public static Model getModelByModelGroup(int modelGroupId) {    Session session = null;    boolean openSession = session == null;    Transaction tx = null;    if (openSession) {        session = SessionFactoryHelper.getSessionFactory().getCurrentSession();         tx = session.getTransaction();    }    String responseMessage = "";    try {        if (openSession) {            tx.begin();        }        Query query = session.createQuery("from Model where modelGroup.id = :modelGroupId");        query.setParameter("modelGroupId", modelGroupId);        List<Model> modelList = (List<Model>)query.list();         Model model = null;        for (Model m : modelList) {            if (m.getModelType().getId() == 3) {                model = m;                break;            }        }        if (model == null) {            Object[] arrModels = modelList.toArray();            if (arrModels.length == 0) {                throw new RuntimeException("Non esiste");            }            model = (Model)arrModels[0];            if (openSession) {                tx.commit();            }            return model;        } catch(RuntimeException ex) {            if (openSession) {                tx.rollback();            }            ex.printStackTrace();            if (responseMessage.compareTo("") == 0) {                responseMessage = "Error" + ex.getMessage();            }            return null;                }    }}

但仍然会出现相同的错误。我已经阅读了很多有关此错误的内容,并找到了一些可能的解决方案。其中之一是将lazyLoad设置为false,但是不允许这样做,因此建议我控制会话

答案1

小编典典

这是错误的,因为你在提交事务时将会话管理配置设置为关闭会话。检查是否有以下内容:

<property name="current_session_context_class">thread</property>

在你的配置中。

为了克服此问题,你可以更改会话工厂的配置或打开另一个会话,而仅要求提供那些延迟加载的对象。但是我在这里建议的是在getModelByModelGroup本身中初始化此惰性集合并调用:

Hibernate.initialize(subProcessModel.getElement());

当你仍处于活动状态时。

最后一件事。一个友好的建议。你的方法中有以下内容:

for (Model m : modelList) {    if (m.getModelType().getId() == 3) {        model = m;        break;    }}

请安装此代码,只需在上面几行的查询语句中过滤ID等于3的那些模型。

答案2

小编典典

如果使用Spring将类标记为@Transactional,则Spring将处理会话管理。

@Transactionalpublic class MyClass {    ...}

通过使用@Transactional,可以自动处理许多重要方面,例如事务传播。在这种情况下,如果调用另一个事务方法,则该方法可以选择加入正在进行的事务,从而避免“无会话”异常。

c# – NHibernate.LazyInitializationException

c# – NHibernate.LazyInitializationException

我们偶尔会出现这个问题,但现在我每次都可以重现它.我在自定义构建的论坛上增加了一个视图计数器,这会导致错误:

NHibernate.LazyInitializationException: Failed to lazily initialize a collection,no session or session was closed

在对象中的另一个集合上发生此错误.如果我添加:

.Not.LazyLoad()

对于我的Fluent映射,错误在我的项目周围移动.我一直禁止对对象的延迟加载,直到它没有延迟加载的地方,然后它抛出了这个错误:

NHibernate.LazyInitializationException: Could not initialize proxy – no Session.

所以,然后我在懒惰的装载上取出了遗嘱,现在我又恢复了原点.当我递增此视图计数器时,它只会出错.这是我的基类保存代码的片段:

using (ISession session = GetSession())
using (ITransaction tx = session.BeginTransaction())
{
       session.SaveOrUpdate(entity);
       tx.Commit();
}

环顾四周,我在另一篇文章中读到这些交易可能会导致问题,但这是因为它们被放置在哪里.此代码扩展到与我的域对象(存储库类)分开的类.这是帖子:

hibernate: LazyInitializationException: could not initialize proxy

我不相信这是我的问题.这是第一个抛出错误的集合的流畅映射.还有其他几个类似的集合.

HasManyToMany(x => x.Votes)
    .WithTableName("PostVotes")
    .WithParentKeyColumn("PostId")
    .WithChildKeyColumn("VoteId");

解决方法

对此问题的进一步研究表明,在单次回调服务器时,如果执行保存并获取,则需要刷新会话.我在保存后进行了冲洗,这似乎纠正了问题.

Hibernate org.hibernate.LazyInitializationException:无法延迟初始化角色集合:

Hibernate org.hibernate.LazyInitializationException:无法延迟初始化角色集合:

我有以下提到的实体类,当我执行我的应用程序时,我得到了以下异常。其他一些类似的问题也不能解决问题。

WARNING: StandardWrapperValve[jersey-serlvet]: PWC1406: Servlet.service()for servlet jersey-serlvet threw exceptionorg.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: test.entity.Dept.empDeptno, no sessionor session was closedat org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:393)       at    org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:385)    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:378)

我该如何解决这个问题?

Emp实体

@Entity@Table(name = "EMP", schema = "SCOTT")@XmlRootElement@NamedQueries({    @NamedQuery(name = "Emp.findAllEmployees", query = "select e from Emp e left     join fetch e.deptNo order by e.empno desc")})public class Emp implements java.io.Serializable {@Id@Column(name = "EMPNO", unique = true, nullable = false, precision = 4,scale = 0)private short empno;@ManyToOne@JoinColumn(name = "DEPTNO", referencedColumnName = "DEPTNO")private Dept deptNo;

部门实体

@Entity@Table(name = "DEPT", schema = "SCOTT")@XmlRootElementpublic class Dept implements java.io.Serializable {@Id@Column(name = "DEPTNO", unique = true, nullable = false, precision = 2,scale = 0)private short deptno;@OneToMany(fetch=FetchType.LAZY,mappedBy = "deptNo")private Set<Emp> empDeptno;

DAOImpl

@Overridepublic List<Emp> findAllEmployees() {  return getEntityManager().createNamedQuery("Emp.findAllEmployees", Emp.class).getResultList();}

泽西岛RESTful服务

 @Component @Path("/employee") public class EmployeeRestService { @Autowired EmployeeService employeeService; @GET @Produces({MediaType.APPLICATION_JSON}) public List<Emp> getEmployees() { List<Emp> emp = new ArrayList<Emp>(); emp.addAll(getEmployeeService().findAllEmployees()); return emp; }

springapplicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">    <!-- Data Source Declaration -->        <bean id="DataSource">        <property name="jndiName" value="jdbc/scottDS"/>       </bean>    <context:component-scan base-package="net.test" />    <bean/>    <bean id="entityManagerFactory">        <property name="dataSource" ref="DataSource" />        <property name="packagesToScan" value="net.test" />        <property name="jpaVendorAdapter">            <bean>                <property name="showSql" value="false" />                <property name="generateDdl" value="false" />                <property name="databasePlatform" value="${jdbc.dialectClass}" />            </bean>        </property>    </bean>    <bean id="defaultLobHandler"/>      <!-- Transaction Config -->    <bean id="transactionManager">        <property name="entityManagerFactory" ref="entityManagerFactory" />    </bean>    <tx:annotation-driven transaction-manager="transactionManager"/>              <context:annotation-config/>    <bean id="hibernateStatisticsMBean">        <property name="statisticsEnabled" value="true" />        <property name="sessionFactory" value="#{entityManagerFactory.sessionFactory}" />    </bean></beans>

答案1

小编典典

我已通过在web.xml中添加以下内容解决了该问题

<filter><filter-name>OpenEntityManagerInViewFilter</filter-name><filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class></filter><filter-mapping><filter-name>OpenEntityManagerInViewFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>

在这里和这里礼貌

谢谢

Hibernate-避免LazyInitializationException-从代理和会话中分离对象

Hibernate-避免LazyInitializationException-从代理和会话中分离对象

MyObject myObject = repositoryHibernateImpl.getMyObjectFromDatabase();
//transaction is finished, and no, there is not an option to reopen it
ThirdPartyUtility.doStuffWithMyObjectType( myObject );

至此,您已经定义了什么是懒惰的并且渴望加载的,并且第三方实用程序将尝试调用“
myObject”实例上的所有方法,这很好,因为您不想为懒惰地返回任何东西加载的属性,不幸的是它不会返回null,而是抛出LazyInitializationException

发生这种情况是因为您实际上是在对象的Hibernate代理上调用该方法,并且知道该方法尚未获取该数据,并引发异常。

甚至有可能获得具有null值的基础对象,以便getter仅返回null,并且不会引发异常?基本上是分离对象,以便Hibernate完全不再意识到它。延迟加载的对象的访问器必须返回null,它不能返回实际值,我们希望能够将实体转换为POJO,而不必创建看起来像实体的对象并且必须重新映射所有对象。价值观。

答案1

小编典典

假设您有一个字段,可以在getter中:

MyField getMyField() {    if (Hibernate.isInitialized(myField)) {        return myField;    }    return null;}

从org.hibernate.Hibernate的javadoc中:

public static boolean isInitialized(Object proxy):检查代理或持久性集合是否已初始化。

Hibernate中的LazyInitializationException:无法初始化代理-没有会话

Hibernate中的LazyInitializationException:无法初始化代理-没有会话

我通过服务将dao称为

@Override@Transactionalpublic Product getProductById(int id) {    return productDao.getProductById(id);}

在岛上,我得到的产品

@Overridepublic Product getProductById(int id) {    Product p = sessionFactory.getCurrentSession().load(Product.class, id);    System.out.print(p);    return p;}

运行正常,但如果我将dao类更改为

@Overridepublic Product getProductById(int id) {    return sessionFactory.getCurrentSession().load(Product.class, id);}

我得到org.hibernate.LazyInitializationException:无法初始化代理-
没有会话。例外发生在我只是在打印产品的视图层中。我不明白为什么在dao方法中在同一行中返回会导致视图层出现异常,但是如果将其保存在引用中然后返回它,效果很好。

答案1

小编典典

这是一个很好的参考,可让您熟悉.get()和.load()方法的工作方式。

@Overridepublic Product getProductById(int id) {    Product p = sessionFactory.getCurrentSession().load(Product.class, id);    return p;}

session.load()缺省情况下,返回代理对象而不访问数据库。基本上,NoObjectFoundError如果表上没有任何记录,它将返回,否则它将返回一个引用,而不填充实际对象,甚至不会访问数据库。您上面的方法返回一个代理,并且由于它还必须初始化您的对象,因此会话保持打开状态并填充了对象。

@Overridepublic Product getProductById(int id) {    return sessionFactory.getCurrentSession().load(Product.class, id);}

但是在第二种方法中,基本上无需任何初始化即可返回代理。此后会话将关闭,无需任何事先使用。这样就得到了错误。

希望能有所帮助

今天关于Java如何修复org.hibernate.LazyInitializationException-无法初始化代理-没有会话的介绍到此结束,谢谢您的阅读,有关c# – NHibernate.LazyInitializationException、Hibernate org.hibernate.LazyInitializationException:无法延迟初始化角色集合:、Hibernate-避免LazyInitializationException-从代理和会话中分离对象、Hibernate中的LazyInitializationException:无法初始化代理-没有会话等更多相关知识的信息可以在本站进行查询。

本文标签: