shikarishambu
shikarishambu

Reputation: 2269

Hibernate subclass with foreign key relationships

I need some help defining the following object hierarchy/ database relationship in Hibernate

From the object sense – Agent is inherited from Person and Agency is inherited from Organization. they are inherited from Party which can have multiple Addresses associated with it

alt text

The database consists of

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

Upvotes: 2

Views: 2766

Answers (2)

Timo Westkämper
Timo Westkämper

Reputation: 22200

Something like the following could be a start

@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;
}

Upvotes: 4

Inv3r53
Inv3r53

Reputation: 2969

this article could help you. includes src as well.

http://www.ibm.com/developerworks/java/library/j-hibernate/

community page

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html

Upvotes: 4

Related Questions