Web programming

Units WEB1P and WEB2P

Hibernate and JPA

Introduction

Hibernate is a powerful, high performance object/relational persistence and query service. Hibernate lets you develop persistent classes following object-oriented idiom - including association, inheritance, polymorphism, composition, and collections. Hibernate allows you to express queries in its own portable SQL extension (HQL), as well as in native SQL, or with an object-oriented Criteria and Example API.

Hibernate is one of a number of object-relational tools. These are intended to bridge the gap between objects that you represent in your program, and the relations that you conventionally represent them as in a database. Others include:

Hibernate is so good, most of its features have been incorporated into the Java Persistence API (JPA) and become a standard part of the Java language. It is now possible to use the features of Hibernate directly or (more usually) access them via the JPA interface. In the latter case, Hibernate is the persistence provider, providing an implementation of an entity manager for JPA-managed objects.

What in Hibernate is called a session, in JPA is called an Entity Manager.

The reference implementation for JPA is Oracle Toplink.

Entity classes

Hibernate/JPA are used to manage objects of so-called entity classes. In JPA an entity class is denoted by the annotation @Entity. An entity class normally maps onto a single database table.

Simple properties (e.g. String, int/Integer) of the entity class are by default regarded as columns of the table. Properties of the entity class that are references to other objects are normally regarded as being foreign key relationships to other database tables (though Hibernate/JPA will manage all the foreign key relationships for you).

Relationships between objects are normally annotated @OneToOne, @OneToMany or @ManyToMany. Each of these can take attributes defining details of the relationship. Multi-ended relationships are normally based on one of the Java Collection types (e.g. Set, List or Map). Inverse relationships (back from the referred-to object to the referrer) can also be managed.

Hibernate object states

Hibernate defines and supports the following object states:

Operations are available in a Hibernate session or JPA Entity Manager to move between these states. For example, em.persist(obj) will make a transient object persistent (i.e. its value will be stored in the database).

Queries

JPA provides two means of querying a database to return a single or list of objects. These are EJB3-QL (similar to SQL but object-oriented) and criteria. Hibernate provides both of these (its almost identical query language is called HQL) plus a third method - query by example.

Further features

  1. Caching (2 levels)
  2. Automatic generation of unique key values when an object is first persisted
  3. Transactions

Reading

Try it out

 

Last updated by Prof Jim Briggs of the School of Computing at the University of Portsmouth

 
The web programming units include some material that was formerly part of the WPRMP, WECPP, WPSSM and WEMAM units.