-->
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: How to resolve the foreign key relationship?
PostPosted: Tue Jan 16, 2007 11:45 am 
Newbie

Joined: Tue Jan 16, 2007 11:17 am
Posts: 2
Hi Friends,

I have 2 persistence domain objects generated based on the database tables, one is called Account.java and the other is Merchant.java.

My Merchant class's accountOid is an primary foreign key referencing to Account class' primary key accountOid, so in both class I have accountOid attribute.

Here is how the code looks like (in short):

public class Account{

private Integer accountOid;

private String email;

private String password;

//constructors
// getters and setters
}

public class Merchant {

private Integer accountOid;

private String merchantName;

private Account account;

//constructors
// getters and setters
}


Here are the mapping files:

Account.hbm.xml
<hibernate-mapping>

<class name="Account" table="account" lazy="false">

<id name="accountOid" type="java.lang.Integer" column="AccountOID"
>

<generator class="native" />
</id>

<!-- properties -->

</class>
</hibernate-mapping>

Merchant.hbm.xml
<hibernate-mapping>
<class name="Merchant" table="merchant" lazy="false">

<id name="accountOid" type="java.lang.Integer" column="AccountOID" >

<generator class="native" />
</id>

<!-- properties-->

<!-- uni-directional one-to-one association to Account -->
<one-to-one name="account" class="Account" outer-join="auto" constrained="true" />

</class>
</hibernate-mapping>


You may observe that in my Merchant.java, it contains an Account type object, which reflects the business logic, one merchant must have its own unique account assoaciated.
Above code is generated by middlegen, looks pretty fine, but I get a problem that whenever when I want to insert a new pair of Merchant-Account records into my database, I have to set the value of acoountOID field for both Merchant and Account objects, I remember there could be a much better way... which is as follows:

Merchant merchant = new Merchant();
Account account = new Account();
// set attributes for account...
// set attributes for merchant ...

merchant.setAccount(account);

new MerchantDAO.insertNewMerchantAccount(merchant);


and this will automatically resolve the foreign key relationship, records in both account table and merchant table will have the same accountOID.

This is what I remember last time when I saw some code sample, but now I am unable to achieve it.

Could anybody please give me some suggestions? is it something to do with the hbm.xml file? or the key generation type?

Thank you in advance!

HarryX


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 12:07 pm 
Newbie

Joined: Tue Jan 16, 2007 8:58 am
Posts: 17
I think in your mapping, you should first define your id as a foreign key

Code:
<id name="accountOid" column="AccountOID">
        <generator class="foreign">
            <param name="property">Account</param>
        </generator>
</id>


I would start of from there... Maybe others have a more detailed explaination...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 12:14 pm 
Newbie

Joined: Tue Jan 16, 2007 8:58 am
Posts: 17
I forgot to add the one-to-one part

Code:
<one-to-one name="account"
        class="Account"
        constrained="true"/>


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Fri Jan 19, 2007 1:36 am 
Newbie

Joined: Tue Jan 16, 2007 11:17 am
Posts: 2
Thanks Mariec, it works in your way.


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.