【问题标题】:how to solve lazyinitializationexception not using fetch=FetchType.EAGER?如何解决不使用 fetch=FetchType.EAGER 的延迟初始化异常?
【发布时间】:2011-05-14 21:21:14
【问题描述】:

我仍然收到异常lazyinitializationexception。 是的,我知道这意味着,当我或其他人试图访问该集合时,会话已关闭。 不,OpenEntityManagerInViewFilter 不起作用。 是的,@ManyToOne(fetch=FetchType.EAGER) 有帮助,但我不想使用它,因为它会一直自动感染。

我还能怎么做?

附: :我正在使用 HibernateEntityManger 和带有注释类的 jpa。

更新 这是我的代码,首先 我有 4 张桌子: 用户(id、first_name、last_name、email....) 角色(ID,姓名,评论......) users_roles(user_id,role_id) 邮件(id,user_id,subject,message,to_id...)

一个用户可以有多个角色.... 用户实体

@Entity
@Table(name = "USERS")
public class User implements GenericDomain{

    public static final String  _ID = "id";
private Long    id;
private String  firstName;
private String  lastName;
private String  email;  
private Set<Role> roles = new HashSet<Role>(0);

/* Constructors */  
public User() {
}

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
public Long getId() { return this.id; }
public void setId(Long id) { this.id = id; }

@Column(name="FIRST_NAME", nullable = false, length = 64)
@NotEmpty
@Length(min = 4, max = 45)
public String getFirstName() { return this.firstName;   }
public void setFirstName(String firstname) { this.firstName = firstname; }

@Column(name="LAST_NAME", nullable = false, length = 64)
@NotEmpty
@Length(min = 4, max = 45)
public String getLastName() { return this.lastName; }
public void setLastName(String lastname) { this.lastName = lastname; }

@Column(name="EMAIL", unique = false, length = 64)
@Email
@NotEmpty
@Length(min = 4, max = 45)
public String getEmail() { return this.email; }
public void setEmail(String email) { this.email = email; }

@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name = "USERS_ROLES"
    , joinColumns = { @JoinColumn(name = "user_id") }
    , inverseJoinColumns = { @JoinColumn(name = "role_id") }
)
public Set<Role> getRoles() {
    return this.roles;
}
public void setRoles(Set<Role> roles) {
    this.roles = roles;
}

/*@Override toString/equals/hascode  */

}

角色实体

@Entity
@Table(name = "ROLES")
public class Role implements GenericDomain {
private Long    id;
private String  name;
private String  comment;
private Set<User> users = new HashSet<User>(0);

public Role() {
}

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }

@Column(name="NAME", nullable = false, length = 64)
@NotEmpty
@Length(min = 1, max = 32)
public String getName() { return name; }
public void setName(String name) { this.name = name; }

@Column(name="COMMENT", nullable = true, length = 256)
@Length(min = 0, max = 255)
public String getComment() { return this.comment; }
public void setComment(String comment) { this.comment = comment;}

@ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.EAGER)
@JoinTable(
    name = "USERS_ROLES"
    , joinColumns = { @JoinColumn(name = "role_id") }
    , inverseJoinColumns = { @JoinColumn(name = "user_id") }
)
public Set<User> getUsers() {
    return this.users;
}
public void setUsers(Set<User> users) {
    this.users = users;
}

/*@Override toString/equals/hascode  */

}

邮件

@实体 @Table(name = "邮件") 公共类 Mail 实现 GenericDomain{

private Long    id;
private String  mailSubject;
private String  mailContent;
private Long    receiverId;
private User    user = null;

public Mail(){  
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
public Long getId(){    return this.id; }
public void setId(Long id){ this.id = id;}

@Column(name = "MAILSUBJECT", nullable = false, length = 63)
@Length(max = 63)
public String getMailSubject(){ return this.mailSubject;    }
public void setMailSubject(String mailSubject){ this.mailSubject = mailSubject; }

@Column(name = "MAILCONTENT", nullable = true, length = 255)
@Length(max = 255)
public String getMailContent(){ return this.mailContent;    }
public void setMailContent(String mailContent){ this.mailContent = mailContent; }   

@Column(name = "RECEIVERID")
public Long getReceiverId(){    return this.receiverId; }
public void setReceiverId(Long receiverId){ this.receiverId = receiverId;   }   


@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "USER_ID")
@NotNull
public User getUser(){ return this.user;    }
public void setUser(User user){ this.user = user;   }
}

用户控制器

@Controller
@RequestMapping("/admin/user")
@SessionAttributes("user")
public class UserController {

private UserService userService;
private RoleService roleService;

@Autowired
public UserController(UserService userService, RoleService roleService) {
    this.userService = userService;
    this.roleService = roleService;
}
@RequestMapping(value = "edit", method = RequestMethod.GET)
public String editUser(@RequestParam(value="id", required = true) Long id, ModelMap model) {

    model.addAttribute("allRoles", roleService.getAll());
    model.addAttribute("user", userService.getOne(id));
    return "/admin/user/edit";
}    }

邮件控制器

@Controller
@SessionAttributes("mail")
@RequestMapping("/portal/mail")
public class MailController{

@Autowired
private MailService mailService;

@RequestMapping(value = "ajaxLoad", method = RequestMethod.GET)
public @ResponseBody List<Mail> list(@RequestParam(value = "type", required = true) String type){
    return mailService.getUserMails((Long) WebHelper.getPrincipal().getUser().getId(),type);
}   

}

我的 web.xml

<filter>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>   

我的用户edit.jsp

<select >
<c:forEach items="${allRoles}" var="role">
    <option value="${role.id}" <c:if test="${fn:contains(roleSelected, role)}">selected="selected"</c:if> >${role.name}</option>
</c:forEach>
</select>

综上所述,我为用户编辑的.jsp 在lazy=false 下工作正常。 使用 FetchType.EAGER 我无法收到我的任何邮件,我进入了一个循环 stackovrflow,没有 FetchType.EAGER 我得到了那个懒惰的异常。

【问题讨论】:

    标签: java hibernate jpa spring-mvc lazy-loading


    【解决方案1】:

    删除所有渴望并添加它解决了我的问题

        <mvc:interceptors>
        <bean class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">  
            <property name="entityManagerFactory" ref="entityManagerFactory" />  
        </bean>
    </mvc:interceptors>
    

    过滤器无效

    【讨论】:

      【解决方案2】:

      OpenEntityManagerInViewFilterOpenEntityManagerInViewInterceptor 工作。你做错了什么。

      除此之外,您还可以使用Hibernate.initialize(..) 来初始化您的收藏。但手动执行此操作不是首选。详细说明为什么过滤器/拦截器不起作用。

      更新:不是将过滤器映射到模式,而是将其映射到调度程序 servlet。所以不要指定&lt;url-pattern&gt;,而是指定&lt;servlet-name&gt;

      【讨论】:

        【解决方案3】:

        首先,检查你是否真的需要Collection 而不是Set。如果集合中的对象是唯一的,则将变量声明为Set,这解决了LazyInitializationException 90% 的问题。

        【讨论】:

        • 嗨,你已经更新了我的帖子并放了一些代码,我没有使用你可以看到的集合,我使用了 Set,但是惰性异常仍然存在
        猜你喜欢
        • 1970-01-01
        • 2012-07-27
        • 2014-03-21
        • 2012-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多