Hibernate

From Wiki RB4

Introduction

Hibernate is an open source object/relational mapping tool for Java. Hibernate lets you develop persistent classes following common Java idiom - including association, inheritance, polymorphism, composition and the Java collections framework.

Hibernate makes use of persistent objects commonly called as POJO (POJO = "Plain Old Java Object".) along with XML mapping documents for persisting objects to the database layer. The term POJO refers to a normal Java objects that does not serve any other special role or implement any special interfaces of any of the Java frameworks (EJB, JDBC, DAO, JDO, etc...). Hibernate uses runtime reflection to determine the persistent properties of a class. The objects to be persisted are defined in a mapping document, which serves to describe the persistent fields and associations, as well as any subclasses or proxies of the persistent object. The mapping documents are compiled at application startup time and provide the framework with necessary information for a class. Additionally, they are used in support operations, such as generating the database schema or creating stub Java source files. In order to provide a clean POJO programming model, Hibernate hides itself inside of your POJO by using either its own implementation of the JDK collections or by using a CGLIB proxy to surround an object reference, depending on the type of association it is managing. Since object graphs can be quite large, and in some cases infinite, it is mandatory to draw the line somewhere when loading an object and claim these associations as lazy. This deferral means that at some later point, it may be necessary to fetch the associated objects from the database when this line is crossed.

Annotations

To use annotations the java file has to (s. JEE#EJB3.0)

import javax.persistance.*;

Development Process

  1. copy the following libs to the application lib directory (hibernate core, hibernate annotations, hibernate validation):
    ant-antlr-1.6.5.jar
    asm-attrs.jar
    asm.jar
    cglib-2.1.3.jar
    commons-collections-2.1.1.jar
    commons-logging-1.0.4.jar
    dom4j-1.6.1.jar
    ejb3-persistence.jar
    hibernate-annotations.jar
    hibernate-commons-annotations.jar
    hibernate-validator.jar
    hibernate3.jar
    jta.jar
    log4j-1.2.11.jar
    <JDBCDriver>.jar
  2. create the annotated class e.g. Action.java
  3. create the Hibernate configuration file hibernate.cfg.xml
  4. use the persistance classes

Designing the domain model

Entities vs. value types

Hibernate supports a fine-grained domain model (more classes than tables). An object of entity type has its own identity (primary key value). An object reference to an entity ist persisted as a reference in the database (a foreign key value). An entity has its own life cylce, it may exist independently of other entities. Criteria for an entity is:

  • shared references
  • lifecycle dependencies
  • identity

Associations

For a many-end association end the propertie must be of an interface type like java.util.set or java.util.List. Hibernate doesn't manage the associations as EJB 2.1 container does by container managed relationships. You have manage the associations like hibernate wasn't there.

Hibernate Infrastructure

SessionFactory

In most hibernate applications SessionFactory should be instantiated once during application initialization (s. example uweheuer application server.HibernateUtil).

Patterns

One_to_Many with no position

@OneToMany(cascade = {CascadeType.ALL}, mappedBy = "parent")

One_to_Many List

in the one class:

@OneToMany(cascade = {CascadeType.ALL})	
@JoinColumn(name = "menu_id", nullable = false)
@IndexColumn(name = "menu_list_position")	// list order      
private List<Urlx> urls = new ArrayList<Urlx>()

in the many class:

@ManyToOne
@JoinColumn(name = "menu_id", nullable = false, updatable = false, insertable = false)
private Menux  menu;

Zero_or_One_to_Many List

in the zero or one class:

@OneToMany(cascade = {CascadeType.ALL})	
@JoinColumn(name = "parent_id", nullable = true) // nullable is true
@IndexColumn(name = "parent_list_position")	 // list order            
private List<Menux> menus = new ArrayList<Menux>();

in the many class:

@ManyToOne
@JoinColumn(name = "parent_id", nullable = true, updatable = false, insertable = false)
private Menux  parent;

Debugging Concepts

SQL Dump

Add the following entries to hibernate.cfg.xml:

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>

then all SQL statements are written to stdout.

Hibernate Configuration

hibernate.cfg.xml

<hibernate-configuration>
 <session-factory>
  <property name="hbm2ddl.auto">[update|create]</property>