我有两个表,我想从数据库中获取更新数据。
用户表(列):

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


user_roles表(列):

role_id - role


我想在users.jsp中列出用户。让我们看看我的代码:
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;
    }
}


角色.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

<?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

<?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>


这部分来自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;
}


我已经声明了公共列表list();在UserDAO界面中。
这是我从控制器发送用户列表到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;
}


在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>


因此,当我将此项目作为服务器运行时,我得到以下输出:

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_


然后这个错误:

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


有人可以解释我的程序有什么问题吗?如果要添加或编辑用户,我该怎么办?

最佳答案

默认情况下,所有关联在Hibernate中都是惰性的(与JPA相对,默认情况下,一对一关联是急切的)。

使UserRole渴望(lazy="false")之间的关联:

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


或显式初始化您打算在会话边界之外使用的惰性关联:

@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());
}


然后,在users.jsp中,应该使用${user.role.role}来访问该值。

关于java - 无法初始化代理-没有 session ( Spring - hibernate -一对一),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31513930/

10-15 11:14