Hi All,
I have these two access classes
Code:
package comm.dao;
import org.hibernate.*;
import javax.persistence.*;
import comm.utils.*;
import org.apache.commons.logging.*;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Query;
import comm.models.*;
import java.util.List;
public class AuthorDAO {
public Author SaveAuthor(Author author) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
List<Author> testList = session.createCriteria(Author.class).add(Restrictions.eq("email", author.getEmail())).list();
Author myAuthor;
if (testList != null && testList.size() > 0) {
myAuthor = testList.get(0);
} else {
myAuthor = author;
}
session.saveOrUpdate(myAuthor);
tx.commit();
session.flush();
session.close();
return myAuthor;
}
public List<Author> ListAuthors(int intStartOffset, int endOffset) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction newTransaction = session.beginTransaction();
Query q = session.createQuery("from Author");
q.setFirstResult(intStartOffset);
q.setMaxResults(endOffset);
List<Author> list = q.list();
newTransaction.commit();
session.close();
return list;
}
public List<Author> ListAuthors() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction newTransaction = session.beginTransaction();
Query q = session.createQuery("from Author");
List<Author> list = q.list();
newTransaction.commit();
session.close();
return list;
}
}
And
Code:
package comm.dao;
import org.hibernate.*;
import javax.persistence.*;
import comm.utils.*;
import org.apache.commons.logging.*;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Query;
import comm.models.*;
import java.util.List;
public class MessageDAO {
public Message SaveMessage(Message message) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(message);
tx.commit();
session.flush();
session.close();
return message;
}
public List<Message> ListMessages(int intStartOffset, int endOffset) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction newTransaction = session.beginTransaction();
Query q = session.createQuery("from Message");
q.setFirstResult(intStartOffset);
q.setMaxResults(endOffset);
List<Message> list = q.list();
newTransaction.commit();
session.close();
return list;
}
public List<Message> ListMessages() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction newTransaction = session.beginTransaction();
Query q = session.createQuery("from Message");
List<Message> list = q.list();
newTransaction.commit();
session.close();
return list;
}
}
And two Model classes mapped to hibernate
Code:
package comm.models;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;
public class Author implements Serializable {
private Long id;
private String name;
private String nick;
private String password;
private String email;
private String address;
private String selftext;
private Date dateOfBirth;
private Date dateOfJoin;
private Set<Asset> assets;
private Set<Message> messages;
public Author() {
}
public Set<Asset> getAssets() {
return assets;
}
public void setAssets(Set<Asset> assets) {
this.assets = assets;
}
public Set<Message> getMessages() {
return messages;
}
public void setMessages(Set<Message> messages) {
this.messages = messages;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Date getDateOfJoin() {
return dateOfJoin;
}
public void setDateOfJoin(Date dateOfJoin) {
this.dateOfJoin = dateOfJoin;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSelftext() {
return selftext;
}
public void setSelftext(String selftext) {
this.selftext = selftext;
}
}
and
Code:
package comm.models;
import java.io.Serializable;
public class Message implements Serializable {
private Long id;
private String text;
private Author messageAuthor;
private Asset messageAsset;
public Message() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Asset getMessageAsset() {
return messageAsset;
}
public void setMessageAsset(Asset messageAsset) {
this.messageAsset = messageAsset;
}
public Author getMessageAuthor() {
return messageAuthor;
}
public void setMessageAuthor(Author messageAuthor) {
this.messageAuthor = messageAuthor;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
and in my servlet I have written this method
Code:
private List dosomething() {
MessageDAO messageAccess = new MessageDAO();
AuthorDAO authorAccess = new AuthorDAO();
String authEmail = "test@test.com";
Author myAuthor = new Author();
myAuthor.setName("Supratim");
myAuthor.setDateOfBirth(new Date());
myAuthor.setEmail(authEmail);
myAuthor.setDateOfJoin(new Date());
myAuthor.setNick("sup");
myAuthor.setPassword("testpass");
myAuthor = authorAccess.SaveAuthor(myAuthor);
Message message = new Message();
message.setText("Test Message");
message.setMessageAuthor(myAuthor);
messageAccess.SaveMessage(message);
List<comm.models.Message> messages = messageAccess.ListMessages(0, 10);
logger.info(messages.size() + " message(s) found:");
System.out.println("Total Messages by "+myAuthor.getEmail()+"::"+myAuthor.getMessages().size());
return messages;
}
Problem is when I call myAuthor.getMessages().size() .. it fails lazy loading.. saying
Code:
SEVERE: failed to lazily initialize a collection of role: comm.models.Author.messages, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: comm.models.Author.messages, no session or session was closed
How can I fix this problem.
Thanks in advance.