Beginner |
|
Joined: Tue Nov 18, 2003 12:34 am Posts: 39 Location: Dallas, Texas, US
|
Hi,
I have two entities associated one-to-one with no foreign key constraint. I have trouble with "cascaded" loading. The first time I run the following Test program, the cascaded load works fine. The second time onwards the cascaded load does not work.
In the test program below, st2_loaded is not-null in the first run (which is what I expected) but in the second run onwards it is null. Remember, I am always loading StudentTest that has the ID "1".
It appears to be fairly simple and straight forward but I don't know what is going on. I appreciate any help on this.
STUDENTTEST.htm.xml
---------------------------
<class
name="StudentTest"
table="Table1"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="int"
unsaved-value="0"
>
<generator class="hilo">
<param name="table">IDGenerator</param>
<param name="column">next_hi</param>
<param name="max_lo">100</param>
</generator>
</id>
<property
name="name"
type="string"
update="true"
insert="true"
column="name"
/>
<one-to-one
name="studentTest2"
class="StudentTest2"
cascade="all"
outer-join="auto"
constrained="false"
/>
</class>
StudentTest2.hbm.xml
-------------------------
<class
name="StudentTest2"
table="Table2"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="int"
unsaved-value="0"
>
<generator class="hilo">
<param name="table">IDGenerator</param>
<param name="column">next_hi</param>
<param name="max_lo">100</param>
</generator>
</id>
<property
name="name"
type="string"
update="true"
insert="true"
column="name"
/>
<one-to-one
name="studentTest"
class="StudentTest"
cascade="none"
outer-join="auto"
constrained="false"
/>
</class>
Test program:
----------------
public static void main(String[] args) throws HibernateException
{
Configuration cfg = new Configuration();
SessionFactory sf = null;
Session session = null;
cfg.addClass(StudentTest.class);
cfg.addClass(StudentTest2.class);
sf = cfg.buildSessionFactory();
session = sf.openSession();
Transaction trans = session.beginTransaction();
StudentTest st = new StudentTest();
StudentTest2 st2 = new StudentTest2();
st.setStudentTest2(st2);
st2.setStudentTest(st);
session.save(st);
trans.commit();
System.out.println("st : " + st.getId());
System.out.println("st2: " + st2.getId());
StudentTest st_loaded = (StudentTest) session.load(StudentTest.class, new Integer(1));
System.out.println("st2_loaded " + st_loaded.getStudentTest2());
session.close();
}
|
|