-->
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: Mapping several 1-N relations to the same entity
PostPosted: Wed Mar 21, 2007 9:51 am 
Beginner
Beginner

Joined: Fri Nov 28, 2003 6:57 am
Posts: 20
How can I map several one-to-many relations to a single entity? For instance, consider you have entities Person and Account (there are no subtypes of account). Each account belongs to a single person. A person can have several accounts, but they may play different roles. E.g., a person can have n private accounts and n business accounts.

The Java code looks quite straightforward:

Code:
public class Person {
   List<Account> privateAccounts;
   List<Account> businessAccounts;
   // getters and setters
}

public class Account {
   Person person;
   // getters and setters
}


How can I map such a situation, where I have two 1-n relations to the same class?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 11:53 am 
Beginner
Beginner

Joined: Fri Nov 28, 2003 6:57 am
Posts: 20
Nobody an idea?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 1:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what is the problem ? It is an assocation like anything else (you might need a 'where' limitation to distinguish them if they are in the same table)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: Mapping several 1-N relations to the same entity
PostPosted: Mon Dec 17, 2007 9:41 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
thorstenschaefer wrote:
How can I map several one-to-many relations to a single entity?

Code:
public class Person {
   List<Account> privateAccounts;
   List<Account> businessAccounts;
   // getters and setters
}

public class Account {
   Person person;
   // getters and setters
}


How can I map such a situation, where I have two 1-n relations to the same class?


max wrote:
what is the problem ? It is an assocation like anything else


Sorry, I have the same problem and I can't follow your "what is the problem". The problem is 1) if the class Person is loaded by Hibernate, it has to look into the Account table chasing the foreign keys for the Person. But how can it decide which Account goes to 'privateAccounts' and which goes to 'businessAccounts'?

2) if I map this example with annotations, i would do it like this:
in class Person:
@OneToMany(mappedBy = "person)
List<Account>privateAccounts;
@OneToMany(mappedBy = "person)
List<Account> businessAccounts;

and in class Account i would have:
@ManyToOne
private Person person;

So, the two different roles would be mapped by the same property! I cannot image how Hibernate can distinguish in this situation.

My thoughts now go to model the different roles as different subclasses. But this would be just artificiell, just caused by a mapping problem.

Is there a solution? Or do I miss something in my understanding of Hibernate/JPA mappings?

Carlo


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 10:15 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Look at it from the other perspective. How would you model this using a relational database? For example:

Table Person
Id Primary Key

Table Account
AccountId
AccountHolderId FK referencing Person
AccountType

So here we use an account type column to distinguish between Private and Business accounts. Obviously this is a prime candidate for an entity hierarchy with AccountType as the discriminator. You then use:
List<PrivateAccount> privateAccounts
List<BusinessAccount> businessAccounts
in your Person entity.

IMO very logical, nothing artifical about such a model.

It gets slightly more complicated if you need to model multiple account holders. You then need to use intermediate tables for a many-to-many mapping:

Table Person
Id Primary Key

Table Account
Account Id
AccountType

Table AccountHolders
AccountId FK referencing Account
PersonId FK referencing Person
Primary Key (AccountId, PersonId)

But the basic idea to have a structure like
public abstract Account ...
public PrivateAccount extends Account ...
public BusinessAccount extends Account ...
remains.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 10:34 am 
Newbie

Joined: Mon Dec 17, 2007 8:16 am
Posts: 2
malm66 wrote:
Look at it from the other perspective. How would you model this using a relational database? For example:

Table Person
Id Primary Key

Table Account
AccountId
AccountHolderId FK referencing Person
AccountType

So here we use an account type column to distinguish between Private and Business accounts. Obviously this is a prime candidate for an entity hierarchy with AccountType as the discriminator. You then use:
List<PrivateAccount> privateAccounts
List<BusinessAccount> businessAccounts
in your Person entity.

IMO very logical, nothing artifical about such a model.


Logical if we are from the relational background. Though the OP's problem is more 'natural' for people without the normal forms hammered into their head. In other words, I would have the same question. since we are talking about a more 'tree' like model(as otherwise, why don't I just go back to my nth normal form strict SQL instead of inserting yet another layer ORM), I would also like to know the answer.

EDIT:

I think the mappedby has to be dropped and the reverse ManyToOne also needs to be dropped. A natural design like this can only do uni-directional(from person to account).


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.