0

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);
}
shintoZ
  • 311
  • 2
  • 12

3 Answers3

2

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!

Balaji Reddy
  • 5,576
  • 3
  • 36
  • 47
0

You get the LazyInitializationException when trying to access a lazy fetched atttribute on an entity detached from the persistence context.

It usually means that your hibernate session ( / JPA entityManager) have been already closed when you access the lazy attribute.

see Struggling to understand EntityManager proper use

Community
  • 1
  • 1
Gab
  • 7,869
  • 4
  • 37
  • 68
0

Actually I dont want to load the machineTemplates data. So I did like

public Set<MachineTemplate> getMachineTemplates() {

    if(Hibernate.isInitialized(machineTemplates))
        return machineTemplates;
    return null;
}
shintoZ
  • 311
  • 2
  • 12
  • In some point the collection has to be initlized when it is need. The only way to init the collection is within session. So you have to keep the session alive till end of your operation and close it by yourself. But this implementation will lead to NullPointerExcepction when you access the collection. – Balaji Reddy Apr 03 '14 at 10:27
  • I tested the code, it works for all cases... We wont get a NullPointerException. – shintoZ Apr 03 '14 at 10:50