Reputation: 311
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);
}
Upvotes: 0
Views: 494
Reputation: 311
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;
}
Upvotes: 0
Reputation: 5700
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!
Upvotes: 2
Reputation: 8323
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
Upvotes: 0