-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: one-to-one load trouble. please help.
PostPosted: Wed Feb 11, 2004 12:01 pm 
Beginner
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();
}


Top
 Profile  
 
 Post subject: an addition
PostPosted: Wed Feb 11, 2004 12:16 pm 
Beginner
Beginner

Joined: Tue Nov 18, 2003 12:34 am
Posts: 39
Location: Dallas, Texas, US
I closed & reopened the session right after commit and before loading st. Now the st2_loaded is null even in the first run if that helps you to help me.
Thanks again,
Madhan.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 12:17 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Reread the one-to-one section of the reference guide. Ids must be the same : either use foreign or assigned generator.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 12:52 pm 
Beginner
Beginner

Joined: Tue Nov 18, 2003 12:34 am
Posts: 39
Location: Dallas, Texas, US
Thanks for your prompt response! I read the hibernate docs again and found that I needed foreign unique key for my application. I added the tag "property-ref" and it worked fine.

BUT, I am using xdoclets and the current version of xdoclet does not have for support "property-ref" attribute in @hibernate tag
(part of the reason why it didn't occur me to investigate more options on the hibernate side!) I will try to see if I can convince xdoclets forum that we need this attribute. Thank you.

Madhan.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.