On Java Development

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

Archive for the ‘JPA annotations’ 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

Guidelines for Creating JPA POJOs, Interfaces and DAOs

without comments

Introduction

This post presents guidelines to follow when creating JPA oriented POJOs, Interfaces and DAOs and briefly covers naming as well as JPA annotations.
Read the rest of this entry »

Written by admin

October 10th, 2014 at 6:56 am

A One-To-One Association Using JPA Annotations

without comments

Introduction

This post shows how to use JPA annotations to define a one-to-one association between files that have a parent/child relationship and which share the same primary key value.
Read the rest of this entry »

Written by admin

July 22nd, 2014 at 6:48 am

Posted in JPA annotations

Java Persistence Annotations

without comments

Introduction

The set of annotations presented in this post are part of the Java Persistence API and originate from the javax.persistence package.
Read the rest of this entry »

Written by admin

July 14th, 2014 at 5:00 am

Posted in JPA annotations

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 »

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