Red Hat Training

A Red Hat training course is available for Red Hat JBoss Web Server

4.2. POJO Example

4.2.1. A Simple POJO Example

Most Java applications require a persistent class representing felines. For example:
package eg;
import java.util.Set;
import java.util.Date;

public class Cat {
    private Long id; // identifier

    private Date birthdate;
    private Color color;
    private char sex;
    private float weight;
    private int litterId;

    private Cat mother;
    private Set kittens = new HashSet();

    private void setId(Long id) {;
    public Long getId() {
        return id;

    void setBirthdate(Date date) {
        birthdate = date;
    public Date getBirthdate() {
        return birthdate;

    void setWeight(float weight) {
        this.weight = weight;
    public float getWeight() {
        return weight;

    public Color getColor() {
        return color;
    void setColor(Color color) {
        this.color = color;

    void setSex(char sex) {;
    public char getSex() {
        return sex;

    void setLitterId(int id) {
        this.litterId = id;
    public int getLitterId() {
        return litterId;

    void setMother(Cat mother) {
        this.mother = mother;
    public Cat getMother() {
        return mother;
    void setKittens(Set kittens) {
        this.kittens = kittens;
    public Set getKittens() {
        return kittens;
    // addKitten not needed by Hibernate
    public void addKitten(Cat kitten) {
    kitten.setLitterId( kittens.size() ); 
The four main rules of persistent classes are explored in more detail in the following sections.

4.2.2. Implement a No-argument Constructor

Cat has a no-argument constructor. All persistent classes must have a default constructor (which can be non-public) so that Hibernate can instantiate them using Constructor.newInstance(). It is recommended that you have a default constructor with at least package visibility for runtime proxy generation in Hibernate.

4.2.3. Provide an Optional Identifier Property

Cat has a property called id. This property maps to the primary key column of a database table. The property might have been called anything, and its type might have been any primitive type, any primitive "wrapper" type, java.lang.String or java.util.Date. If your legacy database table has composite keys, you can use a user-defined class with properties of these types (see the section on composite identifiers later in the chapter.)
The identifier property is strictly optional. You can leave them off and let Hibernate keep track of object identifiers internally. We do not recommend this, however.
In fact, some functionality is available only to classes that declare an identifier property:
  • Transitive reattachment for detached objects (cascade update or cascade merge).
  • Session.saveOrUpdate()
  • Session.merge()
We recommend that you declare consistently-named identifier properties on persistent classes and that you use a nullable (i.e., non-primitive) type.

4.2.4. Prefer Optional Non-final Classes

A central feature of Hibernate, proxies, depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods.
You can persist final classes that do not implement an interface with Hibernate. You will not, however, be able to use proxies for lazy association fetching which will ultimately limit your options for performance tuning.
You should also avoid declaring public final methods on the non-final classes. If you want to use a class with a public final method, you must explicitly disable proxying by setting lazy="false".

4.2.5. Declare Optional Accessors and Mutators for Persistent Fields

Cat declares accessor methods for all its persistent fields. Many other ORM tools directly persist instance variables. It is better to provide an indirection between the relational schema and internal data structures of the class. By default, Hibernate persists JavaBeans style properties and recognizes method names of the form getFoo, isFoo and setFoo. If required, you can switch to direct field access for particular properties.
Properties need not be declared public - Hibernate can persist a property with a default, protected or private get / set pair.