Problems with Hibernate, the owning Session was closed
Hello!This is probably a tricky question that I hope some one could give some hints about.
I get a strange Exception:
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
I get it at the line "customer.setsCountry(country.getName());" in this code:
package website.controller.action;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import website.controller.form.*;
import website.model.hibernate.*;
import website.tools.*;
public class NewCustomerFormAction extends Action
{
public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
NewCustomerForm newCustomerForm = (NewCustomerForm) form;
Customer customer = new Customer();
customer.setsFName(newCustomerForm.getsFName());
customer.setsSName(newCustomerForm.getsSName());
customer.setsPassword(newCustomerForm.getsPassword1());
customer.setsEmail(newCustomerForm.getsEmail());
customer.setsAddress(newCustomerForm.getsAddress());
customer.setsPostal(newCustomerForm.getsPostal());
customer.setsCity(newCustomerForm.getsCity());
Long countryId = new Long(newCustomerForm.getsCountryId());
Country country = CountryService.getCountry(countryId);
customer.setsCountry(country.getName());
customer.setsPhone(newCustomerForm.getsPhone());
customer.setnBYear(new Short(newCustomerForm.getnBYear()));
customer.setnBMonth(new Short(newCustomerForm.getnBMonth()));
customer.setnBDay(new Short(newCustomerForm.getnBDay()));
customer.setsCountryId( Integer.parseInt( newCustomerForm.getsCountryId() ) );
HttpSession session = request.getSession();
session.setAttribute("customer", customer);
return mapping.findForward("success");
}
}
It looks like it has to do with Hibernate and loading classes that I do in this code:
package website.tools;
import java.util.*;
import website.tools.*;
import website.model.hibernate.*;
import org.hibernate.*;
public class CountryService
{
public static synchronized Country getCountry(Long id)
{
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Country country = (Country) session.load(Country.class, id);
//System.out.println( country.getName() );
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
HibernateUtil.getSessionFactory().close();
return country;
}
}
The funny thing is that if I uncomment this line:
//System.out.println( country.getName() );
It works!!!
But of course I do not want to do like that and I do not understand way. I guess Hibernate just load the parent instance, name is considered like a child or member I guess, but it is not in any relation table. The Country class looks like:
package website.model.hibernate;
public class Country
{
private Long id;
private String name;
public Long getId()
{
return id;
}
private void setId(Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String a)
{
name = a;
}
}
The hbm.xml file looks like:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="website.model.hibernate.Country" table="countries">
<id name="id" column="id">
<generator class="increment"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
So if any one could give some hint or if you guys could suggest a better solution then the CountryService-class. Perhaps I have done a bad design??
I hope a have describe this problem in a good way, cause I am not sure what this depends on.
Best regards
Fredrik
