Hibernate和Jersey中的LazyInitializationException [英] LazyInitializationException in Hibernate and Jersey

查看:139
本文介绍了Hibernate和Jersey中的LazyInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我正在做一个使用Hibernate和Jersey的项目。



在服务层,我得到一个' LazyInitializationException '。我搜索了很多关于它。



我看到了创建自定义AccessorType 。但是我仍然得到了例外。



任何人都可以帮助我吗?



Bean:User

  @XmlRootElement 
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlAccessorFactory(XmlAccessorFactoryImpl.class)
public class User {
private String userName;
私人字符串密码;
私人字符串电子邮件;
private String fname;
private String lname;
私人设置< MachineTemplate> machineTemplates;
私人字符串photoUrl;
public User(){
machineTemplates = new HashSet<>();
}
public User(String userName){
this.userName = userName;
}
public User(String userName,String password,String email,String fname,
String lname){
this.userName = userName;
this.password =密码;
this.email =电子邮件;
this.fname = fname;
this.lname = lname;
this.machineTemplates = new HashSet<>();
}

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 String getFname(){
return fname;
}
public void setFname(String fname){
this.fname = fname;
}
public String getLname(){
return lname;
}
public void setLname(String lname){
this.lname = lname;
}

public Set< MachineTemplate> getMachineTemplates(){
return machineTemplates;
}

public void setMachineTemplates(Set< MachineTemplate> machineTemplates){
this.machineTemplates = machineTemplates;
}

public String getPhotoUrl(){
return photoUrl;
}
public void setPhotoUrl(String photoUrl){
this.photoUrl = photoUrl;
}

}

DAO Layer方法

  public User get(String uName){
Session session = getSessionFactory ().openSession();
User u =(User)session.get(User.class,uName);
session.close();

服务层方法

  @GET 
@Path(/ {userName})

@Produces(MediaType.APPLICATION_JSON)
public User getUserInfo(@PathParam(userName)String userName){
return userHelper.getUser(userName);
}


解决方案

加载一个会话超时的懒集。这意味着在使用之前需要初始化集合对象。初始化应该在实体设置方法或DAO类中进行。初始化实体的setter方法通常不推荐使用,因为它将实体与hibernate框架结合在一起。所以最好的地方是DAO层。但在这里我只提及你的参考。
试试这个

  public void setMachineTemplates(Set< MachineTemplate> machineTemplates){

Hibernate .initialize(machineTemplates);
this.machineTemplates = machineTemplates;
}

希望这有帮助!


Hi I am doing a project using Hibernate and Jersey.

In the service layer I am getting a 'LazyInitializationException'. I searched a lot about it.

I saw a solution for creating custom AccessorType. But still I am getting the exception.

Can anyone help me??

I am including more details about it.

Bean: User

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlAccessorFactory(XmlAccessorFactoryImpl.class)
public class User {
private String userName;
private String password;
private String email;
private String fname;
private String lname;
private Set<MachineTemplate> machineTemplates;
private String photoUrl;
public User() {
    machineTemplates = new HashSet<>();
}
public User(String userName) {
    this.userName = userName;
}
public User(String userName, String password, String email, String fname,
        String lname) {
    this.userName = userName;
    this.password = password;
    this.email = email;
    this.fname = fname;
    this.lname = lname;
    this.machineTemplates = new HashSet<>();
}

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 String getFname() {
    return fname;
}
public void setFname(String fname) {
    this.fname = fname;
}
public String getLname() {
    return lname;
}
public void setLname(String lname) {
    this.lname = lname;
}

public Set<MachineTemplate> getMachineTemplates() {
    return machineTemplates;
}

public void setMachineTemplates(Set<MachineTemplate> machineTemplates) {
    this.machineTemplates = machineTemplates;
}

public String getPhotoUrl() {
    return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
    this.photoUrl = photoUrl;
}

}

DAO Layer method

public User get(String uName) {
    Session session = getSessionFactory().openSession();
    User u  = (User) session.get(User.class, uName);
     session.close();
}

Service Layer method

@GET
@Path("/{userName}")

@Produces(MediaType.APPLICATION_JSON) 
public User getUserInfo(@PathParam("userName") String userName) {
    return userHelper.getUser(userName);
}

解决方案

The exception says you are trying to load an lazy collection which of out of session. Meaning you need to initialize the collection object before you use. The initialization should happen either in entity setter method or in DAO class. Initializing in setter method of an entity is not recommended usually since it couples your entity with hibernate framework. So best place is DAO layer. But here I have mentioned just for your reference. Try this

public void setMachineTemplates(Set<MachineTemplate> machineTemplates) {

    Hibernate.initialize(machineTemplates);
    this.machineTemplates = machineTemplates;
}

Hope this is helpful!

这篇关于Hibernate和Jersey中的LazyInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆