hibernate延迟初始化问题:LazyInitializationException:懒得初始化一个角色集合

时间:2022-12-21 19:51:53

I want to manage a Transaction in my persistence layer, But when I try to fetch the results lazily I get this error:

我想在我的持久层中管理一个事务,但是当我尝试懒惰地获取结果时,我得到了这个错误:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role

org.hibernate.LazyInitializationException:懒得初始化角色集合

Can I use LockMode or any other way to solve this problem? Can a find a Object by its id without any Transaction?

我可以使用LockMode或任何其他方式来解决这个问题吗?可以通过其ID而无任何事务查找对象吗?

5 个解决方案

#1


Your problem is that the Hibernate session is already closed when you try to access the content. Hibernate cannot load the content without a session. There are usually two ways to mitigate this problem:

您的问题是,当您尝试访问内容时,Hibernate会话已经关闭。没有会话,Hibernate无法加载内容。通常有两种方法可以缓解此问题:

  1. Don't close the session until you are done with the page. This pattern is called "session in view" and can for example be implemented with a servlet filter.

    在完成页面之前,请勿关闭会话。此模式称为“视图中的会话”,并且可以例如使用servlet过滤器实现。

  2. Initialize all contents that you'll need before closing the session. If you want to initialize the whole object you can use Hibernate.initialize(object).

    在关闭会话之前初始化您需要的所有内容。如果要初始化整个对象,可以使用Hibernate.initialize(object)。

edit: You cannot do anything outside of an transaction in Hibernate.

编辑:你不能在Hibernate的事务之外做任何事情。

#2


You can also look at the official solution from hibernate at http://www.hibernate.org/43.html

您还可以在http://www.hibernate.org/43.html查看hibernate的官方解决方案

#3


Typically the problem is that one of the attributes of the object is lazily loaded. One thing you can do is to have it pre-loaded in your query:

通常问题是对象的一个​​属性是延迟加载的。您可以做的一件事是在查询中预先加载它:

from Sale sale where sale.date > :startDate left join fetch sale.product

来自销售促销,其中sale.date>:startDate left join fetch sale.product

This will pre-fetch the sale.product object.

这将预取sale.product对象。

this site has more information: http://www.javalobby.org/articles/hibernate-query-101/.

该网站有更多信息:http://www.javalobby.org/articles/hibernate-query-101/。

-Dave

#4


There are many ways to pre-fetch properties, so they are there after session is closed:

有许多方法可以预取属性,因此在会话关闭后它们就在那里:

  1. Just call appropriate getter. After field is fetched into bean it is there after session is closed.
  2. 只需调用适当的getter。字段被提取到bean后,会话关闭后就在那里。

  3. Use appropriate attribute in bean descriptor in JPA you'd use @OneToMany(fetch = FetchType.EAGER), but there are similar hibernate ways to do it.
  4. 在JPA中的bean描述符中使用适当的属性,你使用@OneToMany(fetch = FetchType.EAGER),但有类似的hibernate方法。

  5. You may initialize field in HQL query (I'm not sure if it works in HQL, but i think it does), look for FETCH INTO keyword.
  6. 您可以在HQL查询中初始化字段(我不确定它是否在HQL中工作,但我认为它确实如此),查找FETCH INTO关键字。

#5


Or just use another ORM ... like Ebean ORM where lazy loading just works :)

或者只是使用另一个ORM ...就像Ebean ORM那样延迟加载才有效:)

#1


Your problem is that the Hibernate session is already closed when you try to access the content. Hibernate cannot load the content without a session. There are usually two ways to mitigate this problem:

您的问题是,当您尝试访问内容时,Hibernate会话已经关闭。没有会话,Hibernate无法加载内容。通常有两种方法可以缓解此问题:

  1. Don't close the session until you are done with the page. This pattern is called "session in view" and can for example be implemented with a servlet filter.

    在完成页面之前,请勿关闭会话。此模式称为“视图中的会话”,并且可以例如使用servlet过滤器实现。

  2. Initialize all contents that you'll need before closing the session. If you want to initialize the whole object you can use Hibernate.initialize(object).

    在关闭会话之前初始化您需要的所有内容。如果要初始化整个对象,可以使用Hibernate.initialize(object)。

edit: You cannot do anything outside of an transaction in Hibernate.

编辑:你不能在Hibernate的事务之外做任何事情。

#2


You can also look at the official solution from hibernate at http://www.hibernate.org/43.html

您还可以在http://www.hibernate.org/43.html查看hibernate的官方解决方案

#3


Typically the problem is that one of the attributes of the object is lazily loaded. One thing you can do is to have it pre-loaded in your query:

通常问题是对象的一个​​属性是延迟加载的。您可以做的一件事是在查询中预先加载它:

from Sale sale where sale.date > :startDate left join fetch sale.product

来自销售促销,其中sale.date>:startDate left join fetch sale.product

This will pre-fetch the sale.product object.

这将预取sale.product对象。

this site has more information: http://www.javalobby.org/articles/hibernate-query-101/.

该网站有更多信息:http://www.javalobby.org/articles/hibernate-query-101/。

-Dave

#4


There are many ways to pre-fetch properties, so they are there after session is closed:

有许多方法可以预取属性,因此在会话关闭后它们就在那里:

  1. Just call appropriate getter. After field is fetched into bean it is there after session is closed.
  2. 只需调用适当的getter。字段被提取到bean后,会话关闭后就在那里。

  3. Use appropriate attribute in bean descriptor in JPA you'd use @OneToMany(fetch = FetchType.EAGER), but there are similar hibernate ways to do it.
  4. 在JPA中的bean描述符中使用适当的属性,你使用@OneToMany(fetch = FetchType.EAGER),但有类似的hibernate方法。

  5. You may initialize field in HQL query (I'm not sure if it works in HQL, but i think it does), look for FETCH INTO keyword.
  6. 您可以在HQL查询中初始化字段(我不确定它是否在HQL中工作,但我认为它确实如此),查找FETCH INTO关键字。

#5


Or just use another ORM ... like Ebean ORM where lazy loading just works :)

或者只是使用另一个ORM ...就像Ebean ORM那样延迟加载才有效:)