Hibernate:懒得初始化一个集合

时间:2014-10-23 16:54:45

标签: java spring hibernate

我收到以下错误:当我尝试保存数据时。

  

org.hibernate.LazyInitializationException:懒得初始化   角色集合:com.test.model.User.userRole,不可以   初始化代理 - 没有会话

我列出了我用过的课程。

User.java:

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "users", catalog = "test")
public class User {

private String username;
private String password;
private boolean enabled;
private Set<UserRole> userRole = new HashSet<UserRole>(0);

public User() {
}

public User(String username, String password, boolean enabled) {
    this.username = username;
    this.password = password;
    this.enabled = enabled;
}

public User(String username, String password, 
    boolean enabled, Set<UserRole> userRole) {
    this.username = username;
    this.password = password;
    this.enabled = enabled;
    this.userRole = userRole;
}

@Id
@Column(name = "username", unique = true, 
    nullable = false, length = 45)
public String getUsername() {
    return this.username;
}

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

@Column(name = "password", 
    nullable = false, length = 60)
public String getPassword() {
    return this.password;
}

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

@Column(name = "enabled", nullable = false)
public boolean isEnabled() {
    return this.enabled;
}

public void setEnabled(boolean enabled) {
    this.enabled = enabled;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<UserRole> getUserRole() {
    return this.userRole;
}

public void setUserRole(Set<UserRole> userRole) {
    this.userRole = userRole;
}

}

UserRole.java:

import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "user_roles", catalog = "test", uniqueConstraints =  UniqueConstraint(columnNames = { "role", "username" }))
public class UserRole{

private Integer userRoleId;
private User user;
private String role;

public UserRole() {
}

public UserRole(User user, String role) {
    this.user = user;
    this.role = role;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_role_id", 
    unique = true, nullable = false)
public Integer getUserRoleId() {
    return this.userRoleId;
}

public void setUserRoleId(Integer userRoleId) {
    this.userRoleId = userRoleId;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "username", nullable = false)
public User getUser() {
    return this.user;
}

public void setUser(User user) {
    this.user = user;
}

@Column(name = "role", nullable = false, length = 45)
public String getRole() {
    return this.role;
}

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

}

我有一个Dao已实施:

@Transactional
@Override
@SuppressWarnings("unchecked")
public List<User> listUsers() {
    return this.sessionFactory.getCurrentSession()
            .createCriteria(User.class).list();
}

我列出了我的控制器中的所有用户&#34; listUsers&#34;方法。并添加model.addObject(&#34; users&#34;,listUsers);。

在视图中,我使用以下代码:

<c:forEach items="${users}" var="user">
    Username: ${user.username}
    Pass:     ${user.password}
    Role:     ${user.userRole}
</c:foreEach>

但我得到了上述错误(因为$ {user.userRole}变量):

那么如何打印出用户的角色(如:普通用户,管理员)?

2 个答案:

答案 0 :(得分:0)

您的会话仅可用于执行Criteria。在此之后,它已关闭且无法使用。代理字段取决于稍后要恢复的会话,但由于会话已关闭,因此您将收到有关LazyInitializationException的异常消息。

要解决此问题,请在会话仍处于打开状态时检索数据,或者不要将此类字段标记为FetchType.LAZY

答案 1 :(得分:0)

根据您对@Luiggi Mendoza答案的评论,您似乎只需要保持会话开放时间更长。看看this answer一个非常相似的问题。解决方案是更改您的配置,使您的会话使用以下内容持续该线程的生命周期:

<property name="current_session_context_class">thread</property>