具有外键关系的Hibernate子类

时间:2010-05-25 16:29:38

标签: java hibernate inheritance mapping joined-subclass

我需要一些帮助来定义Hibernate中的以下对象层次结构/数据库关系

从对象意义上来说 - Agent继承自Person,而Agency则继承自Organization。它们是从Party继承的,可以有多个与之关联的地址

alt text

数据库由

Agent
-ID
-Name
-PartyID (references Party.ID)
Agency
-ID
-Name
-PartyID (references Party.ID)
Address
-AddrID
-PartyID (references Party.ID)
-Street
Party.
-PartyID

2 个答案:

答案 0 :(得分:4)

答案 1 :(得分:4)

以下内容可能是一个开始

@Entity
public class Party {

  @Id
  private BigInteger partyID;

  private String name;

  @OneToMany(mappedBy="party")
  private Set<Address> addresses;

} 

@Entity
public class Organization extends Party {} 

@Entity
public class Person extends Party {} 

@Entity
public class Agency extends Organization {} 

@Entity
public class Agent extends Person {}

@Entity
public class Address{

  @Id
  private BigInteger addressID;

  @ManyToOne
  private Party party;

  private String street;
}