ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Standardizing Java Persistence with the EJB3 Java Persistence API
Pages: 1, 2, 3, 4, 5

Relationships

In a typical domain model, entities are associated with each other or may have relationships between them. The relationship between two entities can be one-to-one, one-to-many, many-to-one, and many-to-many. These relationships can be expressed using OneToOne, OneToMany, ManyToOne, or ManyToMany annotations, respectively. Our example has a bidirectional OneToMany relationship between the Department and Employee entities.



Since we're using field-based access in our entities, we've specified the annotation on the relationship field of the Department entity as follows:

@OneToMany(mappedBy="department") 
protected Collection<Employee> employees ;

For a bidirectional relationship, you must specify the mappedBy element (as above) in the inverse side of the relationship by pointing to the name of the field or property that owns the relationship.

Standardizing O-R Mapping

You can either use Java metadata annotations or XML to do the O-R mapping for the entities. The EJB3 JPA defines several annotations such as Table, SecondaryTable, Column, JoinColumn, and PrimaryKeyJoinColumn for O-R mapping. Refer to the EJB3 JPA specification for all annotations.

As in our example, you can use the Table annotation to define what table the entity is mapped to, as follows:

@Table(name="DEPT") 
public class Department implements Serializable {

EJB3 JPA depends heavily on defaults and hence, if you don't define the table mapping, the persistence provider will assume that the entity is mapped to a table with the same name as the entity class (DEPARTMENT, in our example). If your entity is mapped to more than one table, you can use the SecondaryTable annotation.

You can use the Column annotation to map a persistent field or property to a database column, like this:

@Column(name="DNAME") 
protected String name;

Here, DNAME is the name of the column to which the persistent field name is mapped. If you don't define O-R mapping with the Column annotation, the persistence engine will try to save its state in a column with the same name as the field or property name.

Entity Inheritance

EJB3 JPA supports several methods of entity inheritance. It requires two types of inheritance table-mapping strategies: Single-table-per-entity hierarchy strategy and Joined-Subclass strategy. The optional table-per-class hierarchy is best avoided.

The single-table-per-entity (SINGLE_TABLE) hierarchy strategy allows all entities in the hierarchy to map to a single table. In our example, FullTime and Contractor extend Employee, and all of these can be mapped to a single table named EMP. In other words, all data related to Employee, FullTime, and Contractor is stored in the same table.

If you use the Joined Subclass strategy, you can store the common persistent data in the table to which the superclass is mapped (such as Employee), and you can create tables for each subclass in the hierarchy to store persistent fields specific to the subclass.

You must use the Inheritance annotation in the superclass to specify the inheritance type, as in the following code. This example shows the entity hierarchy using the single-table-per-entity hierarchy strategy.

 @Entity 
 @Table(name="EMP") 
 @Inheritance(strategy=InheritanceType.SINGLE_TABLE) 
 @DiscriminatorColumn(name="EMPLOYEE_TYPE",
                discriminatorType=DiscriminatorType.STRING, length=1) 
 public abstract class Employee implements Serializable { 
 ...
 }

Each subclass must specify the discriminator value used for that entity type, as follows:

@Entity 
@DiscriminatorValue(value="F") 
public class FullTime extends Employee { 
@Column(name="SAL") 
protected Double salary; 
@Column(name="COMM") 
protected Double commission; 
@Column(name="DESIG")
protected String designation;
...
}

Pages: 1, 2, 3, 4, 5

Next Pagearrow