简体   繁体   English

org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话?

[英]org.hibernate.LazyInitializationException: could not initialize proxy - no Session?

I am trying to make a simple load from an object from the DB, but i got the error "could not initialize proxy - no Session", Any idea ? 我试图从数据库中的对象做一个简单的加载,但我得到错误“无法初始化代理 - 没有会话”,任何想法? Thanks 谢谢

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.jav a:167)
    org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
    org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
    com.myapp.domain.User_$$_javassist_0.getLogin(User_$$_javassist_0.java)
    com.myapp.validator.UserFormValidator.validate(UserFormValidator.java:34)

@Component 
public class UserFormValidator implements Validator {

@Autowired
private UserDAO userDAO;  

@Override
public boolean supports(Class<?> clazz) {
    return UserForm.class.equals(clazz);
}

public UserDAO getUserDAO() {
    return userDAO;
}

public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
}

@Override
public void validate(Object target, Errors errors) {
    User user = (User)getUserDAO().findById(new Integer(1));
    System.out.println ("User -> " + user.getLogin());
}
}

@Transactional
public class GenericDAOHibernateImpl <T, PK extends Serializable>
implements GenericDAO<T, PK> {

public GenericDAOHibernateImpl() {
    super();
}

private Class<T> type;

@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;

public GenericDAOHibernateImpl(Class<T> type) {
    this.type = type;
}

public void saveOrUpdate(T object) {
    getSession().save(object);
}

public Object findById(Serializable id) {
     return getSession().load(type, id);
}

protected Session getSession() {
    return sessionFactory.getCurrentSession();
}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        ">

<context:property-placeholder location="/WEB-INF/jdbc.properties" />

<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />   

<!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
<!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->                           
<!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html -->
<!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
             p:dataSource-ref="dataSource"
             p:configLocation="classpath:hibernate.cfg.xml"
             p:packagesToScan="com.myapp.domain"/>  

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
destroy-method="close" 
p:driverClass="${jdbc.driverClassName}" 
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"
p:acquireIncrement="5"
            p:idleConnectionTestPeriod="60"
            p:maxPoolSize="100"
            p:maxStatements="50"
            p:minPoolSize="10" 
/>


<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
            p:sessionFactory-ref="sessionFactory" />


@Entity
@Table(name="USER")
public class User extends appEntity {

@Id
@Column(name = "USER_ID", unique = true, nullable = false)
private Integer id;
@Column(name = "LOGIN")
private String login;
@Column(name = "PASSWORD")
private String password;


public String getLogin() {
    return login;
}
public void setLogin(String login) {
    this.login = login;
}

public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

Try to add @Transactional to the validate method: 尝试将@Transactional添加到validate方法:

@Override
@Transactional(readOnly=true)
public void validate(Object target, Errors errors) {
    ...
}

What happens is that because there is no @Transactional annotation, there is no session associated to the method, and each query will run in it's own session that is closed immediately afterwards. 会发生什么是因为没有@Transactional注释,没有与该方法关联的会话,并且每个查询将在其自己的会话中运行,该会话之后立即关闭。

The method session.load() always returns a proxy, unlike session.get() (see here for differences between load vs get ). session.load()方法总是返回一个代理,与session.get() 不同 (请参阅此处了解load与get之间差异 )。

So the proxy is returned, but due to the missing @Transactional the session that created the proxy is immediately closed. 因此返回代理,但由于缺少@Transactional ,创建代理的会话立即关闭。 When the proxy is accessed the first time, it's session is closed so we get the 'no session' error. 当第一次访问代理时,它的会话已关闭,因此我们得到“无会话”错误。

If you change from load() to get() that will only partially solve the problem, because if after the get you try to load for example a lazy initialized collection, the exception occurs again. 如果从load()更改为get()只会部分解决问题,因为如果在尝试加载之后尝试加载例如延迟初始化集合,则会再次发生异常。

Adding @Transactional to the business method will ensure the presence of the same session for the duration of the method call, and prevent the occurrence of this and other related errors. @Transactional添加到业务方法将确保在方法调用期间存在相同的会话,并防止出现此错误和其他相关错误。

暂无
暂无

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

相关问题 休眠问题:org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - Hibernate issue: org.hibernate.LazyInitializationException: could not initialize proxy - no Session (org.hibernate.LazyInitializationException)org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - (org.hibernate.LazyInitializationException) org.hibernate.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 JPA存储库org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - JPA repository org.hibernate.LazyInitializationException: could not initialize proxy - no Session 错误org.hibernate.LazyInitializationException - 无法初始化代理 - 没有会话 - ERROR org.hibernate.LazyInitializationException - could not initialize proxy - no Session org.hibernate.LazyInitializationException:无法初始化代理-无Session。 对于测试用例JUnit - org.hibernate.LazyInitializationException: could not initialize proxy - no Session . For a Testcase JUnit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM