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


AddThis Social Bookmark Button

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

Standardizing O-R Mapping in JPA

Defining Persistent Objects: Entities

An entity is a lightweight domain object--a Plain Old Java Object (POJO) that you want to persist in a relational database. Like any POJO, an entity may be either an abstract or a concrete class, and it can extend another POJO. You can use the javax.persistence.Entity annotation to mark a POJO to be an entity.

Here's how you make the Department object in the domain model an entity:

package onjava;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.*;
@NamedQuery(name="findAllDepartment", query="select o from Department o")
public class Department implements Serializable {
    protected Long deptNo;
    protected String name;
    protected String location;
    protected Collection<Employee> employees;
    public Department() {
    public Collection<Employee> getEmployees() {
        return employees;
      public void setEmployees(Collection<Employee> employees)    {
        this.employees = employees;
    public Employee addEmployee(Employee employee) {
        return employee;
    public Employee removeEmployee(Employee employee) {
        return employee;

Every entity has a primary key; you can use the Id annotation on a persistent field or property to mark it to be the primary key. An entity maintains its state by using either fields or properties (via setter and getter methods). This depends on where you use O-R mapping annotations. The above example uses field-based access; we've used Id annotation with the deptNo field. To use property-based access, you'd mark annotations such as Id with the properties as follows:

public Long getDeptNo() {
        return deptNo;
 public void setDeptNo(Long deptNo) {
        this.deptNo = deptNo;

Remember that the same access type, either field or property, must be used for all entities in an entity hierarchy.

Every field defined in an entity is, by default, persistent in nature; if you don't want to save the state for a field/property, you must define the field/property as a transient by marking it with the @Transient annotation or by using a transient modifier.

Embeddable Objects

An embeddable object is a persistent object with no identity of its own; it's part of another entity. For example, we can assume that Address has no identity of its own and is stored as a part of the Employee entity. Thus, Address is a candidate for an embeddable object.

You can create an embeddable object like this:

 public class Address { 
 protected String streetAddr1; 
 protected String streetAddr2; 
 protected String city; 
 protected String state; 

Here's how to define the object as an embedded object in the target entity:

    public class Employee {
    protected Long id;                                              
    protected Address address;                                      

Pages: 1, 2, 3, 4, 5

Next Pagearrow