On Java Development

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

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.

 
 

Mapping the Header File

The mapping begins with the parent file (IGREQHDR) used to hold data with which to launch a process, portions of which are shown below. The file contains properties that define the process request:

  • RHRHID – REQUEST HEADER ID
  • RHRQID – SOURCE ID
  • RHCTDT – CREATE DATE
  • RHCTTM – CREATE TIME
  • RHUSER – USER NAME
  • RHPSWD – STATUS
  • IGREQDTL – The detail record associated with this request header.

When the process completes, the outcome is placed into the detail record of file IGREQDTL. There is only one detail record for each request header, hence the one-to-one association.

The association between the header and detail file is made beginning on line 32 using JPA’s @OneToOne annotation which says that anytime a header is retrieved, get the detail record and any CRUD operations are to also affect the detail record. For example, when the header is deleted, the detail record will also be deleted.

The @PrimaryKeyJoinColumn annotation is used to identify the primary join column which is the key field i.e. the request header request id. JPA’s @OneToOne and @PrimaryKeyJoinColumn annotations must be associated with the header class’s property that will hold the class representing the detail record, seen on line 34. (private IGREQDTL igreqdtl;)

 
 

Mapping the Detail File

The mapping of the detail file (IGREQDTL) is much simpler as seen below. ALl that is required is to identify the identity field.

Written by admin

July 22nd, 2014 at 6:48 am

Posted in JPA annotations

Leave a Reply

You must be logged in to post a comment.