-->
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 relationship whith generator
PostPosted: Wed Oct 20, 2004 6:35 am 
Newbie

Joined: Wed Oct 20, 2004 6:22 am
Posts: 9
Hello ,
I have been trying to have a one-to-one relationship between 2 objects which have the same primary key. Therefore I user a generator class="foreign". I am expecting when I save the parent (report) that it will automatically inert the child (reportedPatient) but it just saves the parent (report). WHat am I doing wrong? I have no errors in the logs. I have been searching everywhere and I can t see what is my problem.
Thanks for any help

Cecile

Hibernate version: 2.1.6

Mapping documents:
<hibernate-mapping
package="com.roche.dss.domainmodel">
<class name="Report" table="REPORTS" >
<id name="reportId" type="long" column="REPORT_ID">
<generator class="sequence">
<param name="sequence">SQ_REPR_REPORT_ID</param>
</generator>
</id>
<property name="caseId" type="string" column="CASE_ID"/>
<property name="sequenceNumber" type="long" column="SEQ_NUM"/>
<property name="employeeId" type="long" column="EMPLOYEE_ID"/>
<property name="internalCaseId" type="string" column="INTERNAL_CASE_ID"/>
<property name="originCode" type="string" column="ORIGIN_CODE"/>
<property name="reportType" type="string" column="REPORT_TYPE_CODE"/>
<one-to-one name="reportedPatient" cascade="save-update"/>
</class>
<class name="ReportedPatient" table="PATIENT_DETAILS">
<id name="reportId" column="REPORT_ID">
<generator class="foreign">
<param name="property">report</param>
</generator>
</id>

<one-to-one name="report" constrained="true" cascade="all"/>
<property name="genderCode" type="string" column="GENDER_CODE"/>

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Report report=new Report();
ReportedPatient reportedPatient=new ReportedPatient();
report.setReportedPatient(reportedPatient);
reportedPatient.setReport(report);

report.setCaseId(Long.toString(caseId));
report.setEmployeeId(1);
report.setInternalCaseId("2");
report.setOriginCode("SPON");//SPON or STUD
report.setReportType("AE");
report.setSequenceNumber(1);
reportedPatient.setGenderCode("M");
session.save(report);



Name and version of the database you are using:
Oracle 9i


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 20, 2004 1:44 pm 
Newbie

Joined: Mon Aug 30, 2004 1:37 am
Posts: 16
Cecile,

your mapping looks fine - I tried it out using hsql (not Oracle, so so my id generator differs from your mappings) and it worked as it should. The problem appears to be that you are not commiting the transaction. You need to surround your unit of work by a transaction:
Code:
    public void runTest(SessionFactory sf)throws Exception {
        long caseId = 999;
        Session session =  sf.openSession();
        Transaction tx = null;       
        try {
                       
            tx = session.beginTransaction();  //NB begin the unit of work
           
            Report report=new Report();
            ReportedPatient reportedPatient=new ReportedPatient();
            report.setReportedPatient(reportedPatient);
            reportedPatient.setReport(report);

            report.setCaseId(Long.toString(caseId));
            report.setEmployeeId(1);
            report.setInternalCaseId("2");
            report.setOriginCode("SPON");//SPON or STUD
            report.setReportType("AE");
            report.setSequenceNumber(1);
            reportedPatient.setGenderCode("M");

            session.save(report);

            tx.commit();    //NB commit the unit of work

        } catch (Exception e){
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (Exception e2){
                    System.out.println("runTest rollback failed" + e2);
                     throw e2;
                }
            }
            System.out.println("runTest failed: " + e);
            throw e;
        } finally {
            try {
                if (session.isOpen()) {
                    session.close();
                }
            }catch (Exception e3){
                throw e3;
            }
        }
        System.out.println("At end of runTest");

    }


The Hibernate Session disables auto commit on the connection so you need to commit the transaction yourself.
I hope that this solves your problem. Good luck!

Grainne.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 4:37 am 
Newbie

Joined: Wed Oct 20, 2004 6:22 am
Posts: 9
Hi Grainne,
I did the transaction myself but I forgot to include it in the code sample. ANy other idea? Could it be an oracle compatibility problem?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 8:33 am 
Newbie

Joined: Wed Oct 20, 2004 6:22 am
Posts: 9
If anyone has that problem.
The problem was that the foreign id generated was a long so the default value was 0. there are 2 solutions:
1) change the id type from long to a Long object
2) add in your mapping of the id :
<id name="id" type="long" column="uid" unsaved-value="0">


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.