-->
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.  [ 10 posts ] 
Author Message
 Post subject: How make two one-to-one relations to inherited classes?
PostPosted: Fri Mar 05, 2004 3:15 pm 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
I have 2 inherited classes.
LimitedAccount extends Account, these classes are correctly stored with hibernate, in the same table using discriminator.

Now I need 2 instances in other class called Application
in the java code is this way:
Code:
private Account bankAccount;
private LimitedAccount communityAccount;


How can I map these relations?????


Thanks alll!!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 3:18 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
give details on the relationnal model and fk


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 3:35 pm 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
I have two tables: members and accounts

Application instance is a row in members and accounts has Account and LimitedAccount
instances.

the class Account is mapped as:
Code:
<class name="nl.strohalm.cyclos.entities.accounts.Account" table="accounts" discriminator-value="account">
    <id name="id" type="long" column="account_id">
         <generator class="native"/>
    </id>
    <discriminator column="subclass" type="string" force="true"/>
      
    <property name="creationDate" column="creationDate" type="calendar"/>
    <property name="lastAmount" column="lastAmount" type="float"/>

    <many-to-one name="owner" column="owner_id" class="nl.strohalm.cyclos.entities.accounts.IAccountOwner"/>
       
    <subclass name="nl.strohalm.cyclos.entities.accounts.LimitedAccount" discriminator-value="limited_account"/>
</class>


So I have owner field as a FK. Application, Member and other classes implements IAccountOwner interface too.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 3:45 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
does a member have an Account AND a LimitedAccount or
a member have an Account OR a LimitedAccount (more logic no?)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 3:52 pm 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
Application is a special member and it has a Account AND LimitedAccount instances!!

Code:
private Account bankAccount;
private LimitedAccount communityAccount;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 3:54 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
so in your member table, you might have two field, something like accountId and limitedAccountId isn't it?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 4:06 pm 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
No, becouse is better to have a FK into account table setting a PK in member table.

This way:
Code:
accounts table
+------------+------------------+---------------------+------------+----------+
| account_id | subclass         | creationDate        | lastAmount | owner_id |
+------------+------------------+---------------------+------------+----------+
|          1 | account          | 2004-03-05 16:49:14 |          0 |        1 |
|          2 | limited_account  | 2004-03-05 16:49:14 |          0 |        1 |


Code:
members table
+-----------+---------------+
| member_id | subclass      |
+-----------+---------------+
|         1 | application   |


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 4:14 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
the way you can do it is:
declare on the member mapping file a one(member) to many(accounts) using a collection, this collection will always contains 2 accounts, one typed Account, the other typed LimitedAccount.

So the attribute mapped is List accounts
but you can write 2 other attributes
Account account
LimitedAccount limitedAccount and implement the getter and setter of these two attributes...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 4:21 pm 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
Ok, is this???

<one-to-many class="Account">

And Hibernate will return both accounts(Account and LimitedAccount)??


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 4:51 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
look doc about one to many, you'll have to declare something like that
<set name="accounts >
<key="owner_id"/>
<one-to-many class="nl.strohalm.cyclos.entities.accounts.Account"/>
</set>

But on your Member class, the property that is really mapped will be a Collection accounts

and as i said this Collection, in your case, will contain one Account and one LimitedAccount so if you want to have a getAccount() and a getLimitedAccount (and i suppose setters too), you'll have to code these methods...

Maybe there is a way to map your classes differently, but i don't know how


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