简体   繁体   English

无法初始化代理-没有会话(春季休眠一对一)

[英]could not initialize proxy - no Session (Spring-Hibernate-one to one)

I have two tabeles and I want to fetch on update data from database. 我有两个表,我想从数据库中获取更新数据。
users table(columns): 用户表(列):

user_id - username - password - role_id(Foreign Key) - email

user_roles table(columns): user_roles表(列):

role_id - role

I want to list users in users.jsp . 我想在users.jsp中列出用户。 lets see my codes: 让我们看看我的代码:
User.java User.java

package com.terafast.manager.model;

public class User {
    private int id;
    private String username;
    private String password;
    private String email;
    private Role role;

    public User() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }
}

Role.java 角色.java

package com.terafast.manager.model;

public class Role {
    private int id;
    private String role;

    public Role() {

    }

    public Role(String role) {
        this.role = role;
    }

    public long getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

User.hbm.xml User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.terafast.manager.model">
    <class name="User" table="users">
        <id name="id" column="USER_ID">
            <generator class="native" />
        </id>
        <property name="username" column="USERNAME" />
        <property name="password" column="PASSWORD" />
        <property name="email" column="EMAIL" />

        <many-to-one name="Role" class="com.terafast.manager.model.Role"
            unique="true" not-null="true" column="role_id" />
    </class>
</hibernate-mapping>

Role.hbm.xml Role.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.terafast.manager.model">
    <class name="Role" table="user_roles">
        <id name="id" column="role_id">
            <generator class="native" />
        </id>
        <property name="role" />
    </class>
</hibernate-mapping>

This part is from UserDAOImpl that create List of users: 这部分来自UserDAOImpl,它创建用户列表:

@Override
@Transactional
public List<User> list() {
    @SuppressWarnings("unchecked")
    List<User> listUser = (List<User>) sessionFactory.getCurrentSession().createCriteria(User.class)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

    return listUser;
}

I already declared public List list(); 我已经声明了public List list(); in UserDAO interface. 在UserDAO界面中。 This is the part that I send List of users to users.jsp from my Controller: 这是我从控制器发送用户列表到users.jsp的部分:

@RequestMapping("/users/show")
public ModelAndView handleRequest() throws Exception {
    List<User> listUsers = userDao.list();
    ModelAndView model = new ModelAndView("panel/users");
    model.addObject("userList", listUsers);
    return model;
}

In jsp file I list Users like this: 在jsp文件中,我列出了这样的用户:

<c:forEach var="user" items="${userList}" varStatus="status">
    <tr>
        <td>${status.index + 1}</td>
        <td>${user.username}</td>
        <td>${user.email}</td>
        <td>${user.role}</td>
        <td><a href="edit?id=${user.id}">Edit</a>
            &nbsp;&nbsp;&nbsp;&nbsp; <a href="delete?id=${user.id}">Delete</a>
        </td>
    </tr>
</c:forEach>

So when I run this project as server I got this output: 因此,当我将此项目作为服务器运行时,我得到以下输出:

Hibernate: select this_.USER_ID as USER_ID1_1_0_, this_.USERNAME as USERNAME2_1_0_, this_.PASSWORD as PASSWORD3_1_0_, this_.EMAIL as EMAIL4_1_0_, this_.role_id as role_id5_1_0_ from users this_

and then this error: 然后这个错误:

Jul 20, 2015 3:32:06 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Could someone explain what is wrong with my program? 有人可以解释我的程序有什么问题吗? And how can if I want to add or edit user, what should I do exactly? 如果要添加或编辑用户,我该怎么办?

By default, all associations are lazy in Hibernate (as opposed to JPA where to-one associations are eager by default). 默认情况下,所有关联在Hibernate中都是惰性的(与JPA相对,默认情况下急于一对一关联)。

Either make the association between User and Role eager ( lazy="false" ): 使UserRole渴望之间建立关联( lazy="false" ):

<many-to-one name="Role" class="com.terafast.manager.model.Role"
   lazy="false" unique="true" not-null="true" column="role_id" />

or explicitly initialize the lazy associations which you intend to use out of the session boundaries: 或显式初始化您打算在会话边界之外使用的惰性关联:

@SuppressWarnings("unchecked")
List<User> listUser = (List<User>) sessionFactory.getCurrentSession().createCriteria(User.class)
   .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

for (User user : listUser) {
   Hibernate.initialize(user.getRole());
}

Then in users.jsp, you should use ${user.role.role} to access the value. 然后,在users.jsp中,应该使用${user.role.role}来访问值。

Your exception totally depends on Lazy and Eager loading. 您的例外情况完全取决于懒惰和渴望的加载。 Set the <many-to-one> relation with property of lazy with false in your user.hbm.xml file. user.hbm.xml文件user.hbm.xml lazy属性设置为<many-to-one>关系,并设置为false

try this: 尝试这个:

<many-to-one name="Role" class="com.terafast.manager.model.Role"
   lazy="false" fetch="select" unique="true" not-null="true" column="role_id" />

and follow @Dragan Bozanovic answer. 并按照@Dragan Bozanovic的回答。

暂无
暂无

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

相关问题 Hibernate / Spring3:无法初始化代理 - 没有Session - Hibernate/Spring3: could not initialize proxy - no Session Hibernate无法初始化代理 - 没有Session - Hibernate could not initialize proxy - no Session Spring JPA - org.hibernate.LazyInitializationException:无法初始化代理 - 无 Z71AB3B3AE294B3ABDE46 - Spring JPA - org.hibernate.LazyInitializationException: could not initialize proxy - no Session Spring DATA JPA + Hibernate-无法初始化代理-修复后无会话: - Spring DATA JPA + Hibernate - could not initialize proxy - no Session after fix: 错误:休眠无法初始化代理-没有会话 - Error: Hibernate could not initialize proxy - no Session Hibernate 4 + Spring Data CrudRepository,CLI应用程序:无法延迟初始化集合:无法初始化代理-没有会话 - Hibernate 4 + Spring Data CrudRepository, CLI application: failed to lazily initialize a collection: could not initialize proxy - no Session Hibernate 延迟加载不适用于 Spring 启动 =&gt; 无法延迟初始化角色集合无法初始化代理 - 没有 Session - Hibernate Lazy loading not working with Spring Boot => failed to lazily initialize a collection of role could not initialize proxy - no Session 休眠延迟异常无法初始化代理-无会话 - hibernate lazy exception could not initialize proxy - no Session Hibernate LazyInitializationException:无法初始化代理-没有会话 - Hibernate LazyInitializationException: could not initialize proxy - no Session Hibernate中的LazyInitializationException:无法初始化代理 - 没有Session - LazyInitializationException in Hibernate : could not initialize proxy - no Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM