-->
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.  [ 6 posts ] 
Author Message
 Post subject: foreign generator and one-to-one relationship
PostPosted: Tue Aug 26, 2003 7:56 pm 
Newbie

Joined: Tue Aug 26, 2003 7:46 pm
Posts: 3
Hello,

I'm having trouble establishing a simple one-to-one relationship using a foreign key.
This is not well documented in the Hibernate2 documentation, but I did find this in the tips and tricks section:

Code:
<generator class="foreign">
     <param name="property">parent</param>
</generator>


However, what is parent supposed to be? This is what i have currently, but when i do a simple save(chart), no record is inserted into the table.

Code:
<class name="com.catalepse.patientity.models.Person" table="PERSON">

        <id name="personId" type="long"  column="PERSON_ID" unsaved value="null" >
                <generator class="identity"/>
        </id>

        <joined-subclass name="Patient" table="PATIENT">
                <key column="PERSON_ID"/>
                <one-to-one name="chart" cascade="all" constrained="true"/>
        </joined-subclass>
</class>

<class name="Chart" table="CHART">

        <id name="personId" type="integer"  column="PERSON_ID" unsaved-value="null" >
                 <generator class="foreign">
                        <param name="property">patient</param>
                </generator>
        </id>
            
        <one-to-one name="patient"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2003 9:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
That looks correct.

(Are you sure you remembered to flush()?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 12:00 pm 
Newbie

Joined: Tue Aug 26, 2003 7:46 pm
Posts: 3
when would I do a flush? There are only two seperate transactions that occur:

Code:
Patient patient = new Patient();
Chart chart = new Chart();

patient.setChart(chart);
chart.setPatient(patient);

//THIS IS THE FIX I HAVE TO GET IT TO WORK WITH AN ASSIGNED ID
//chart.setPersonId((int)patient.getPersonId());

PatientService.savePatient(patient);
ChartService.saveChart(chart);


PatientService and ChartService looks like this:

Code:
Session session = FactoryPlant.getSession();
Transaction transaction = session.beginTransaction();

session.save(patient);

transaction.commit();
session.close();


Also, do I need to explicitly save the patient and the chart? Or should saving the patient cascade and insert the new chart?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 1:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Saving the Patient will cascade save to the Chart, as long as patent.getChart() returns an instance of Chart.

Transaction.commit() does flush the session.


What is your unsaved-value mapping?



I suggest that, with this kind of problem, you enable the Hibernate log4j log and see what Hibernate is doing.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 2:50 pm 
Newbie

Joined: Tue Aug 26, 2003 7:46 pm
Posts: 3
After two days of fiddling, I got it to work. The log wasn't saying anything special, it would insert an entry into person, then one into patient, but nothing into chart.

I set the unsaved-value of chart to "any", and it worked. This seems a little odd though... should it not still work if the unsaved-value was "null"? Is this a bug in Hibernate (assigning a key to chart before checking to see if its unsaved?) Either way, it should be better documented.

Thanks for the help,
Sean Dunn


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 3:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, it looks to me as if you are using a primitive type for the id. So it should be unsaved-value="0", as per the hibernate documentation.


Bug in _Hibernate_? I doubt that *very* much.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.