-->
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.  [ 7 posts ] 
Author Message
 Post subject: Help with one-to-one mapping
PostPosted: Mon Feb 19, 2007 6:57 pm 
Newbie

Joined: Mon Feb 19, 2007 2:53 pm
Posts: 4
Hibernate version: 3.2
Name and version of the database you are using: DB2 (8.1)

Hi,

I have the following scenario:
-> A Transaction table (with transaction_id as it's pk)
-> An MQ_Transaction table having a transaction_id column (where, the transaction_id in the MQ_Transaction table = the transaction_id in the Transaction table) and vendor_id column

This is what I want to do:
I want to have a one-to-one association between my Transaction table and MQ_Transaction table.
So that, while updating my fields in the Transaction table, I also want MQ_Transaction to be one of the fields in the mapping. This is what it looks like:

(I am using a sequence ID generator)
Transaction txn = new Transaction();
txn.setVendorID("xyz");
txn.setRequestTime("abcd");
MQTxn mqTxn = new MQTxn();
mqTxn.setSomething ("something");
//Please note that I did not set any transactionID for the mqTxn object.
txn.setMQTxn (mqTxn);
open session;
saveOrUpdate;
commit;

And of course, I'm getting an error saying that I have to explicitly set the transactionID field for the MQ_Transaction table.

This is what I'm not understanding how to do:
Since I'm using a sequence ID generator, my Transaction table is being updated with the proper transactionID. But, what I want is the same transactionID to be updated to the MQ_Transaction table's transactionID field as well.

Any help please? I can provide more information if I was not clear, and please excuse me if I was not clear enough.

Thanks,
-mpochampalli


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 7:06 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Try performing a save of mqTxn before
txn.setMQTxn(mqTxn)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 7:17 pm 
Newbie

Joined: Mon Feb 19, 2007 2:53 pm
Posts: 4
Hi Somu,

If I do a save on the txn object, and then set the mqTxn object to the txn object, it works (but of course, even then, I have to explicitly set the transactionID from the txn object to the mqTxn object's txnID field).

But what I want is for hibernate to generate and update mqTxn with the same ID that has been generated for the Transaction table.

Thank you.
-mp


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 7:36 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
I think there is some issue with your java files. (because mqTxn class need not have a txnId field in it. What it should have is just a reference to txn class)

Your java file should be similar to this

class Txn {
private Long txnId;
}

class MQTxn {
private Long mqTxnId;
private Long vendorId;
private Txn txn;
}

Can you post your java files?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 7:38 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Extension to above reply

Then do a save of Txn as

txn.saveUpdate(); // This step generates the txnId

and then

mqTxn.setTxn(txn);
mqTxn.save();

That would solve.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 12:32 pm 
Newbie

Joined: Mon Feb 19, 2007 2:53 pm
Posts: 4
Thanks Somu,

What I want is for the MQ_Transaction table to have the same PK that the Transaction table has, withouth explicitly setting it. The Transaction table has a Primary Key generated because I am using a sequence generator. But what about MQ_Transaction? What property should I use for the MQ_Transaction table, so that it can have the same Primary Key that the Transaction table had?

Following is my Transaction.hbn.xml:
Code:
<hibernate-mapping auto-import="true" default-lazy="true">
<class
    name="TransactionVO"
    table="cdhc.TRANSACTION"
>   
    <id
        name="transaction_id"
        type="java.lang.String"
        column="transaction_id"
    >
     <generator class="sequence">
      <param name="sequence">CDHC.TRANSACTION_ID_SEQ</param>
     </generator>
    </id>
   
   <property
        name="vendor_id"
        type="java.lang.String"
        column="vendor_id"
        not-null="true"
        length="50"
    />

    <one-to-one
       name="mq_transaction"
       class="MQ_TransactionVO"
       cascade="save-update"
     />
</class>
</hibernate-mapping>

Following is my MQ_Transaction.hbn.xml:
Code:
<hibernate-mapping auto-import="true" default-lazy="false">
<class
    name="MQ_TransactionVO"
    table="cdhc.MQ_TRANSACTION"
>
     <id
        name="mq_transaction_id"
        type="java.lang.String"
        column="transaction_id"
    >
    </id>
   
     <one-to-one
         name="transaction"
         class="TransactionVO"
    />
   
    <property
        name="vendor_id"
        type="java.lang.String"
        column="vendor_id"
        not-null="false"
        length="24"
    />
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 9:39 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Use foreign for MQ_Transaction. I am not aware of the exact syntax though (sorry about that)


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