JPA I:N and 1:1 using Junction table
JPA Question: I am trying to model 1:N (Person & Address) relationship using a junction table (Person_Address).
Person (personId PK)
Address (addressId PK)
PersonAdd开发者_高级运维ress ( personId, addressId composite PK, personId FK references Person, addressid FK references Address)
How to map these relationships? How to load list of addresses when Person object is loaded?
You're describing a OneToMany relationship which typically does not require a join table. You just have a field on the Many side that references the One side.
However, if you really want to do what you're doing, then this should work:
@Entity
public class Person {
...
@OneToMany
@JoinTable(
name="PersonAddress",
joinColumns = @JoinColumn( name="personId"),
inverseJoinColumns = @JoinColumn( name="addressId")
)
public Set<Address> getAddresses() {...}
...
}
精彩评论