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