开发者

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).

  1. Person (personId PK)

  2. Address (addressId PK)

  3. 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() {...}

...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜