Standardizing Java Persistence with the EJB3 Java Persistence API

Standardizing Java Persistence with the EJB3 Java Persistence API

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.*;

@Entity

@NamedQuery(name="findAllDepartment", query="select o from Department o")

@Table(name="DEPT")

public class Department implements Serializable {

    @Id

    @Column(nullable=false)

    protected Long deptNo;

    @Column(name="DNAME")

    protected String name;

    @Column(name="LOC")

    protected String location;

    @OneToMany(mappedBy="department")

    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) {

        getEmployees().add(employee);

        employee.setDepartment(this);

        return employee;

    }

    public Employee removeEmployee(Employee employee) {

        getEmployees().remove(employee);

        employee.setDepartment(null);

        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:

@Id

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:

@Embeddable 

 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:

    @Entity

    public class Employee {

    @Id

    @GeneratedValue(strategy=GenerationType.AUTO)

    protected Long id;                                              

    ...

    @Embedded

    protected Address address;                                      

    ...

}

Prev  [1] [2] [3] [4] [5] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java: None
  • Now: Tutorial for Web and Software Design > Java > JavaBeans > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction