On Java Development

All things related to Java development, from the perspective of a caveman.

Archive for the ‘Hibernate’ Category

Persisting Records with Auto Generated Fields

without comments

Introduction

This post shows what can happen when adding records to a file with an identity field and the value of for the field is automatically incremented by the database.

There are certain annotations that need to be made to tell Hibernate not to update that field.

 
 

The SQL Statement Segment

Below is the source segment used to create the table. The Library/FileName is an arbitrary name of the library and file.

The GENERATED ALWAYS AS IDENTITY clause indicates that DB2 will always generate a unique value for the identity column for each row inserted into the table. DB2/400 will issue an error if a value is specified in an insert operation for the identity column defined using this clause. In other words, your Java code should never modify this field. If it does, you should probably expect unexpected consequences for the file.

 
 

The Entity Class

Below is the source segment of the entity class showing the column definition for the identity field

Adding ‘insertable = false, updatable = false’ to the column definition indicates that the responsibility for calculating the sequence number does not belong to the application. DB2/400, or DB2 Universal Database as it is now known, is responsible for maintaining the value for this field. In effect, the field obtains a read only characteristic.

 
 

Errors Generated During Record Insert

Without ‘insertable = false, updatable = false’, when the record is persisted to the database, the following error is generated.

 
 

Resources:

Generating Unique Values in DB2 Universal Database
Apache OpenJPA 2.1 User’s Guide, Chapter 13, Mapping Metadata, Column
IBM Knowledge Center – Apache OpenJPA User’s Guide

Written by admin

November 13th, 2015 at 7:23 am

JPA Entity Manager Factory, Transaction Manager, Datasource and JNDI Configuration

without comments

Introduction

This post describes the contents of the file “jpaDataSourceContext” found in baseproject. This file contains datasource and JNDI definitions using Spring’s JPA support for Entity Manager Factory, Transaction Manager and Hibernate Adapters. The file is read during the initial phase of the application’s startup processing which occurs when the server is started. This means all data source/JNDI references, used or not, must be present and valid.
Read the rest of this entry »

Written by admin

February 19th, 2015 at 10:52 am

Hibernate’s Transparent and Automated Persistence

without comments

Introduction

This post highlights aspects of Hibernate’s record update strategy called “Transparent and automated persistence”

 

Persistent Object States

Hibernate defines four object states: transient, persistent, detached, and removed. The following sections presents details on each state and the methods in Hibernate or JPA that can move an object to that state.

 

Transient Objects

Objects instantiated using the new operator are called transient objects. The Hibernate persistence context isn’t aware of operations performed on transient objects. Their state is lost when they aren’t referenced by any other object and become available for garbage collection. They don’t have any association with any database table row. JPA doesn’t have this state. The figure below shows the creation of a transient object.
hibernateTransientObject

 

Persistent Objects

An object that has a database identity associated with it is called a persistent object. The key for an object to be in a persistent state is to be associated with a persistent context. The persistent context caches the object state and can identify changes made, if any. You can create persistent objects two ways: you can retrieve a database table row as an entity by calling API methods like get(), load(), and list(); or you can persist a transient object using API methods like save() and saveOrUpdate(). The figure below shows the possible ways for an object to become persistent.
hibernatePersistentObject

 

Detached Objects

A persistent object is associated with a persistent context. The persistent context no longer exists after the transaction is closed. If the application retains a reference to the persistent object, then the object is said to be in the detached state. The state of a detached object can be modified and can be persisted back to the database. This is very common when you’re working with applications that have a multilayered architecture. The figure below shows how an object can become detached. Hibernate provides a reattached mode and a merging mode to make a detached object persistent. JPA has merging mode only.
hibernateDetachedObject

 

Removed Objects

A persistent object is considered to be in the removed state when a delete() or remove() operation is called on it. The delete() method is provided by a Hibernate session, whereas the remove() method is provided by the JPA EntityManager. Note that when an object is removed, the persistence context then deletes the object from the database. This means you shouldn’t use any references to the removed object in the application. The figure below shows how this state can be obtained.
hibernateRemovedObject

 

Transparent and Automated Persistence

Entity classes represent physical records in the database. When using an entity manager, which essentially describes Hibernate, entity objects are being managed through different phases of its life-cycle. The following diagram illustrates the life-cycle of an entity with respect to Hibernate’s API.
entityLifeCycle

If a managed entity object is modified within an active transaction the change is detected by the owning EntityManager and the update is propagated to the database on transaction commit.

For example, the following code retrieves an entity from the database and increments its field value. The change of the value occurrs on line 16. This causes the EntityManager to persist the record when the transaction is committed, which occurs in the Service Layer of the application.

Transparent persistence fosters a degree of portability; without special interfaces, the persistent classes are decoupled from any particular persistence solution. Business logic is fully reusable in any other application context. You could easily change to another transparent persistence mechanism. Because JPA follows the same basic principles, there is no difference between Hibernate persistent classes and JPA entity classes. – Java Persistence with Hibernate

Anyone who has written a PHP application will appreciate the above. Using PHP, the application is hard-wired to the underlying database. For example, the connection and CRUD operations differ across the spectrum of vendor databases providing zero possibility for portability without rewriting the DAO layer. Contrasting this against a Java application using Hibernate, the application can operate without modifications to the persistence layer whether it is using a MS-SQL Server, Oracle. DB2 or DB/400.

Written by admin

August 1st, 2014 at 5:47 am

Posted in Hibernate

Creating Summary Data using SQL Count(*) and Hibernate

without comments

Introduction

This post shows how to implement a SQL statement with a Count(*) function using Hibernate. This creates a record set that doesn’t map to an existing entity, so the coding needed to pull the data out of the resulting list and to then put them into a container class is a bit different than usual when using Hibernate and mapped entities.

This post also goes beyond being a simple presentation about an SQL statement inside a DAO. Along the way, it explains many Java development concepts that every developer should know. It also exposes forces that should cause the developer to recognize the need for a certain Java Design Pattern.

Maybe this post should have been called “How to Design an Application to use a Mini-Architecture including Services, Business Objects, Data Access and Transfer Objects while using Hibernate to summarize records.“, but the focus is still about how to use the Count(*) function with Hibernate.
Read the rest of this entry »

Using Hibernate to Update a Record with a Composite Key

without comments

Introduction

This post shows how to use Hibernate to update a single record using a composite key. This approach differs a bit when using a file defined with a identity field, which is a little easier to update code-wise.
Read the rest of this entry »

Written by admin

June 5th, 2014 at 5:00 am

Posted in Hibernate

Using Partial Keys with Hibernate HQL

without comments

Introduction

This post shows how to use a partial key when the key fields are isolated as a composite key under Hibernate.
Read the rest of this entry »

Written by admin

April 10th, 2014 at 8:55 am

Posted in Hibernate

Hibernate Transaction Coding Idiom

without comments

Introduction

This post shows the suggested form of a Hibernate transaction constructed using a try/catch/finally block.
Read the rest of this entry »

Written by admin

April 3rd, 2014 at 6:48 pm

Posted in Hibernate,Idioms

Sorting Records Using JPA Annotations

without comments

Introduction

This post shows how to use JPA Annotations to sort the detail records of a @OneToMany relationship. The details about how to define a @OneToMany relationship can be read in the post titled Describing a Header/Detail Relationship Using JPA Annotations With Hibernate. This post expands on the annotations defined for the files PHSPRRQHD and PHSPRRQDT.
Read the rest of this entry »

Written by admin

February 7th, 2014 at 10:25 am

Describing a Header/Detail Relationship using JPA Annotations With Hibernate

without comments

Introduction

This post shows how to describe a header/detail relationship (HDR) to Hibernate using files that have a composite key and those with identity records.
Read the rest of this entry »

Written by admin

January 30th, 2014 at 2:22 pm

Anatomy of a Web Application

without comments

Introduction

This post is for the reader who is curious about the architecture, components and technologies that make up a web application.
Read the rest of this entry »