Hibernate Persistence Object

Hibernate Java Persistent Entity Object

Save this file in project/src/main/java/com/innotrekker/app/hbm

package com.innotrekker.app.hbm;

import java.util.Date;

public class Order {
	private Long id;
	private String comment;
	private Date date;

	// An constructor with no parameters is required by Hibernate
	public Order() {
	}

	public Order(String comment, Date date) {
		this.comment = comment;
		this.date = date;
	}

	public Long getId() {
		return id;
	}

	// Since application rarely set the ID itself,
	// it is declared as a private method and accessible by Hibernate core library
	private void setId(Long id) {
		this.id = id;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

}
  • A constructor without parameter is required by Hibernate
  • The constructor should be a public method for runtime proxy generation
  • To support lazy-loading, the class cannot declare as final
  • Avoid declaring public final methods as this will limit the use of proxy generation by Hibernate

Implement hash related functions if it will add to a Set object or re-attach to a session

equals()
hashCode()

Implement Inheritance for Hibernate Persistent Entity

SpecialOrder extends an Order

public class SpecialOrder extends Order {
   private String instruction;

   public String getInstruction() {
         return instruction;
   }
   protected void setInstruction(String instruction) {
         this.instruction=instruction;
  }
}

Hibernate Component Mapping

Hibernate component persist its value directly to the same table rather than a reference entity (another table with foreign reference relationship).

Hibernate Persistence Entity

public class Student {
    private Name name;
    ...
    public Name getName() {
        return name;
    }
    public void setName(Name name) {
        this.name = name;
    }
    ...
}

public class Name {
    String firstName;
    String lastName;
    public String getFirstName() {
        ...
    }
    void setFirstName(String first) {
        ...
    }
    public String getLastName() {
        ...
    }
    void setLastName(String lastName) {
        ...
    }
}

Map the Hibernate component (Name) as a single object

<class name="Student" table="students">
    ...
    <component name="Name" class="Name">
        <property name="firstName"/>
        <property name="lastName"/>
    </component>
</class>

Map the Hibernate component as a list

<set name="tutors" table="tutor" >
    ...
    <composite-element class="Name">
        <property name="firstName"/>
        <property name="lastName"/>
    </composite-element>
</set>

Misc Terminology

Hibernate dynamic model

  • Developer does not write persistent classes, only mapping files in replacement of the POJO object
  • This option is configurable with the default_entity_mode option for a SessionFactory