简体   繁体   English

休眠实体getter中的LazyInitializationException

[英]LazyInitializationException in Hibernate Entity getter

When I do a Hibernate merge() to a ItemVersionLanguage object, I got "ERROR [org.hibernate.LazyInitializationException] (pool-9-thread-1) could not initialize proxy - no Session: org.hibernate.LazyInitializationException: could not initialize proxy - no Session" from the codes below. 当我对ItemVersionLanguage对象执行Hibernate merge()时,出现“错误[org.hibernate.LazyInitializationException](pool-9-thread-1)无法初始化代理-没有会话:org.hibernate.LazyInitializationException:无法初始化代理-没有会话”。

But when I get data from it, it works fine from either ItemVersion or ItemVersionLanguage's url. 但是,当我从中获取数据时,可以从ItemVersion或ItemVersionLanguage的url正常工作。

I don't have a @Transactional wrapping the code in which merge() locates. 我没有@Transactional包裹merge()所在的代码。

ItemVersionLanguage.java ItemVersionLanguage.java

@Entity
@Table(name = "item_version_language")
public class ItemVersionLanguage implements java.io.Serializable {
   private String url;
   private ItemVersion itemVersion;

   public void setUrl(String url)
   {
      this.url = url;
   } 
   @Column(name = "url")  
   public String getUrl()
   {
      if(this.url == null)
      {
          return this.itemVersion.url; //this results in the problem!
      }
      else
      {
          return this.url;
      }
   }
   public void setItemVersion(ItemVersion itemVersion)
   {
       this.itemVersion = itemVersion;
   }

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "item_version_obj_id", nullable = false)
   public ItemVersion getItemVersion()
   {
       return this.itemVersion;
   }
}

ItemVersion.java ItemVersion.java

@Entity
@Table(name = "item_version")
public class ItemVersion implements java.io.Serializable {
   private String url;

   public void setUrl(String url)
   {
      this.url = url;
   }   
   @Column(name = "url")
   public String getUrl()
   {
      return this.url;
   }
}

Am I doing anything wrong? 我做错什么了吗?

Lazy initialization enables the variable to act as a proxy which can fetch its value as need be. 延迟初始化使变量可以充当代理,可以根据需要获取其值。 This error occurs when code attempts to read from the variable after the session that retrieves the entity has been closed, which makes it impossible for the proxy to lazily fetch its value. 当代码在关闭检索实体的会话之后尝试从变量中读取时,会发生此错误,这使代理无法延迟获取其值。

You can use Hibernate.initialize(itemVersionLanguage.getItemVersion()) to load the proxy's value before the session is closed. 您可以使用Hibernate.initialize(itemVersionLanguage.getItemVersion())在关闭会话之前加载代理的值。

See http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html#performance-fetching-initialization for more details. 有关更多详细信息,请参见http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html#performance-fetching-initialization

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在 JWTAuthorizationFilter 中获取实体,则休眠 LazyInitializationException - Hibernate LazyInitializationException if entity is fetched in JWTAuthorizationFilter Hibernate尝试保存一个分离的实体-LazyInitializationException - Hibernate trying to save a detached entity - LazyInitializationException 调用getter到惰性初始化字段后的org.hibernate.lazyinitializationexception - org.hibernate.lazyinitializationexception after calling getter to a lazy initialised field 休眠-LazyInitializationException - Hibernate - LazyInitializationException org.hibernate.LazyInitializationException:无法初始化代理 - 没有Session,说没有getter,但我的getter / setter存在 - org.hibernate.LazyInitializationException: could not initialize proxy - no Session ,says no getter, but my getter /setters exists 尝试访问实体中的BLOB字段时出现org.hibernate.LazyInitializationException - org.hibernate.LazyInitializationException while trying to access a BLOB field in entity 将实体转换为dto时,Spring MVC,休眠LazyInitializationException - Spring mvc, hibernate LazyInitializationException when converting entity to dto 带 getter 但不带 setter 的休眠实体属性:PropertyNotFoundException - Hibernate entity attribute with getter, but without setter: PropertyNotFoundException 使用ModelMapper的LazyInitializationException(Spring + Hibernate) - LazyInitializationException with ModelMapper (Spring + Hibernate) Hibernate和Jersey中的LazyInitializationException - LazyInitializationException in Hibernate and Jersey
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM