Hello,
Is it possible to set lazy-initialisation of an many-to-one in an component to false?
I tried to do it in the Reservation class, which contains an Stay class(component), and the Stay class contains an Place(on-to-many)
If I lookup an reservation and trie to access the place of the reservation I get an lazy initialisation exception. I've set lazy="false" in the mapping of Reservation.
Could someone tell me if I'm misunderstanding something or is this an restriction of hibernate?
The underlying code is completely stripped down(but working). When I execute the Test class I get an lazy initialization exception.
Can anyone give me an explanation please?
Thanks in advance
The Place class
===================
Code:
public class Place {
private Long id;
private String placeName;
public Long getId(){return this.id;}
public void setId(Long id){this.id=id;}
public String getPlaceName() {return placeName;}
public void setPlaceName(String placeName) {this.placeName = placeName;}
public String toString(){
return placeName;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="be.lmpa.zomerzon.model.Place"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native"/>
</id>
<property
name="placeName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="placeName"
/>
</class>
</hibernate-mapping>
The Stay class
===================
Code:
public class Stay {
private Long id;
private Place place;
public Long getId(){return this.id;}
public void setId(Long id){this.id=id;}
public Place getPlace(){return place;}
public void setPlace(Place place){this.place=place;}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="be.lmpa.zomerzon.model.Stay"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native"/>
</id>
<many-to-one
name="place"
class="be.lmpa.zomerzon.model.Place"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="Place_id"
not-null="true"
/>
</class>
</hibernate-mapping>
The Reservation class
===================
Code:
public class Reservation {
private Long id;
private Stay stay;
public Long getId(){return this.id;}
public void setId(Long id){this.id=id;}
public Stay getStay() {return stay;}
public void setStay(Stay stay) {this.stay = stay;}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="be.lmpa.zomerzon.model.Reservation"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native"/>
</id>
<component
name="stay"
class="be.lmpa.zomerzon.model.Stay"
>
<many-to-one
name="place"
class="be.lmpa.zomerzon.model.Place"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="Place_id"
not-null="true"
[color=cyan]lazy="false"[/color]
/>
</component>
</class>
</hibernate-mapping>
The DAO class
==============
Code:
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
public class DAO {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static void saveOrUpdate(Object obj){
Session hibSession = sessionFactory.openSession();
Transaction tx = hibSession.beginTransaction();
try{
hibSession.saveOrUpdate(obj);
}catch (HibernateException he) {
tx.rollback();
throw he;
}finally {
hibSession.close();
}
}
public static Object getObjectFromClass(Class klasse,Long id){
Object result;
Session hibSession = sessionFactory.openSession();
Transaction tx = hibSession.beginTransaction();
try{
result = hibSession.get(klasse,id);
tx.commit();
}catch (HibernateException he) {
tx.rollback();
throw he;
}finally {
hibSession.close();
}
return result;
}
}
The Test Class
===============
Code:
public class Test {
public static void main(String args[]){
Reservation res;
Place place = new Place();
place.setPlaceName("place 1");
DAO.saveOrUpdate(place);
Stay stay = new Stay();
stay.setPlace(place);
res = new Reservation();
res.setStay(stay);
DAO.saveOrUpdate(res);
Reservation lookedUpReservation = (Reservation)DAO.getObjectFromClass(Reservation.class,res.getId());
System.out.println(lookedUpReservation);
//this line throws: org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
System.out.println(lookedUpReservation.getStay().getPlace());
}
}