简体   繁体   English

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

[英]LazyInitializationException in Hibernate : could not initialize proxy - no Session

I call dao from my service as 我把我的服务称为dao

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

and in the dao I am getting product as 在dao我得到的产品

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

This runs fine but if I change my dao class to 这运行正常,但如果我将我的dao类更改为

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

I get org.hibernate.LazyInitializationException: could not initialize proxy - no Session. 我得到org.hibernate.LazyInitializationException:无法初始化代理 - 没有Session。 The exception occurs in view layer where I am just printing the product. 我正在打印产品的视图层中发生异常。 I do not understand why returning in same line in dao method results in exception in view layer but works fine if I save it in a reference and then return that. 我不明白为什么在dao方法中返回同一行导致视图层中的异常,但如果我将它保存在引用中然后返回它,则工作正常。

Here's a good reference to get you familiar with how .get() and .load() method works. 这里有一个很好的参考 ,让您熟悉.get()和.load()方法的工作原理。

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

session.load() by default returns a proxy object without hitting a database. session.load()默认返回代理对象而不命中数据库。 It basically returns NoObjectFoundError if there aren't any records on the table or else it will return a reference without populating the actual object or even hitting the database. 如果表上没有任何记录,它基本上会返回NoObjectFoundError ,否则它将返回一个引用,而不会填充实际对象甚至命中数据库。 Your above method returns a proxy and since it has to initialize the your object as well, the session remains open and object is populated. 您的上述方法返回一个代理,因为它也必须初始化您的对象,会话保持打开状态并填充对象。

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

But in your second method, basically a proxy is returned without any initialization. 但在第二种方法中,基本上返回代理而不进行任何初始化。 session is closed thereafter without any prior use. 会议在此后关闭,无需事先使用。 Thus you get the error. 因此,你得到错误。

Hope that helps 希望有所帮助

This error means that you're trying to access a lazily-loaded property or collection, but the hibernate session is closed or not available . 此错误意味着您正在尝试访问延迟加载的属性或集合,但hibernate会话已关闭或不可用。 Lazy loading in Hibernate means that the object will not be populated (via a database query) until the property/collection is accessed in code. Hibernate中的延迟加载意味着在代码中访问属性/集合之前,不会填充对象(通过数据库查询)。 Hibernate accomplishes this by creating a dynamic proxy object that will hit the database only when you first use the object. Hibernate通过创建一个动态代理对象来实现这一点,该对象仅在您第一次使用该对象时才会访问数据库。 In order for this to work, your object must be attached to an open Hibernate session throughout it's lifecycle. 为了实现这一点,您的对象必须在整个生命周期中附加到一个打开的Hibernate会话。

If you remove the SOP statement then object is not accessed at all and thus not loaded. 如果删除SOP语句,则根本不访问对象,因此不加载。 And when you try to access it in your other part code of code then it will throw LazyInitializationException. 当您尝试在其他部分代码中访问它时,它将抛出LazyInitializationException。

It's a typical problem when you are dealing with Hibernate and view layer. 当您处理Hibernate和视图层时,这是一个典型的问题。 Error happens because Hibernate session is closed before your view is rendered. 发生错误是因为在呈现视图之前Hibernate会话已关闭。 Two easiest way to fix this is to use Open Session In View pattern OR fetch all data you need in views before view rendering. 解决此问题的两种最简单方法是使用Open Session In View模式或在视图呈现之前获取视图中所需的所有数据。

Since you are using Spring, the first solution is the easiest one - just apply OpenSessionInViewFilter (or OpenEntityManagerInViewFilter if you are using JPA): 由于您使用的是Spring,因此第一个解决方案是最简单的 - 只需应用OpenSessionInViewFilter (如果使用JPA,则应用OpenEntityManagerInViewFilter ):

http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.html http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/jpa/support/OpenEntityManagerInViewFilter.html http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.html http://docs.spring.io/spring/docs/current /javadoc-api/org/springframework/orm/jpa/support/OpenEntityManagerInViewFilter.html

You should also read what are the pros and cons of using OSIV pattern. 您还应该了解使用OSIV模式的优缺点。

in your Product entity, try adding fetch = FetchType.EAGER in your OneToMany relationship annotation, like 在您的Product实体中,尝试在OneToMany关系注释中添加fetch = FetchType.EAGER,例如

@OneToMany(mappedBy = "employee", fetch = FetchType.EAGER) @OneToMany(mappedBy =“employee”,fetch = FetchType.EAGER)

This will load the entire Product object graph, avoiding subsequent calls 这将加载整个Product对象图,避免后续调用

暂无
暂无

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

相关问题 Hibernate LazyInitializationException:无法初始化代理-没有会话 - Hibernate LazyInitializationException: could not initialize proxy - no Session 休眠问题:org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - Hibernate issue: org.hibernate.LazyInitializationException: could not initialize proxy - no Session LazyInitializationException-无法初始化代理-没有会话 - LazyInitializationException - could not initialize proxy - no Session LazyInitializationException:无法初始化代理 - 否 Session - LazyInitializationException: could not initialize proxy - no Session org.hibernate.LazyInitializationException:无法初始化代理-没有会话-很少发生 - org.hibernate.LazyInitializationException: could not initialize proxy - no Session - occurs rarely 如何修复 org.hibernate.LazyInitializationException - 无法初始化代理 - 没有 Session - How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session org.hibernate.LazyInitializationException:无法初始化proxy -no Session - org.hibernate.LazyInitializationException: could not initialize proxy -no Session org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话 - org.hibernate.LazyInitializationException: could not initialize proxy - no Session Spring JPA - org.hibernate.LazyInitializationException:无法初始化代理 - 无 Z71AB3B3AE294B3ABDE46 - Spring JPA - org.hibernate.LazyInitializationException: could not initialize proxy - no Session 多线程抛出 org.hibernate.LazyInitializationException:无法初始化代理 - 否 Session - Multithreading throws org.hibernate.LazyInitializationException: could not initialize proxy - no Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM