tags:

views:

180

answers:

1

Hi there,

suppose I have an entity "Employee", and an entity "Address". An employee has an address associated to him/her. In a relational database sense, the Address table would be considered a weak entity, since an employee address cannot exist if there is no corresponding Employee in the database. Thus, suppose I want to model the following:

Table: Employee

EmployeeID | Name

Table Address

EmployeeID | StreetName

For the Employee table, the primary key is EmployeeID. For the Address table, the primary key is EmployeeID, yet it is also a foreign key to the Employee table. How would you model this in EJB 3.0 using j2ee? When I make an entity class called Employee, I can set the @id annotation above employeeId in order to mark that attribute as the primary key. However when I'm making an Address entity, I want to set the employeeID attribute as both a primary key and a foreign one (relate it to the Employee table). Is there an annotation for this? Hopefully I've made my question clear. Thanks.

db

+3  A: 

It's probably not a good idea to map Address as a separate entity - not only is its lifetime controlled by its owner (e.g. Employee) as you said, but you also don't want to share the same Address instance between different employees.

Consider mapping it as @Embeddable instead:

@Embeddable
public class Address {
  private String streetName;
  ...
}

@Entity
public class Employee {
  @Embedded
  private Address address;
  ...
}

Note that above will map Address columns to Employee table; if you'd rather keep a separate table you can overwrite that using @JoinTable annotation.

ChssPly76