Chapter 4. Data Layer Recipes

Table of Contents

1. Creating a Domain Object / POJO / JavaBean
2. Creating a Domain Model
3. Defining relationships
4. Creating a Data Access Object (DAO)
5. Table and Field Mapping
6. Relationship Mapping
7. Implementing DAO using JPA
8. Generating Domain Model from an existing database
9. Performing Basic CRUD Operations
10. Performing Complex Queries using JPQL
11. Deciding between Defined versus Derived Data Models
12. Working with Related Data
13. Migrating to a Different DBMS

The data layer is also referred to as the persistence layer or domain object layer. The data layer is represented by a domain model; a distinct set of inter-related application objects that embody the functionality and characteristics of the system being built. This layer also encompasses the persistence of the domain model to and from a database.

The tools for defining the data layer in the Spring DSL are the Domain Object and Data Access Object artifacts, and they will be described in detail in a following chapter.

1. Creating a Domain Object / POJO / JavaBean

PROBLEM

For application frameworks like the Spring Framework that utilizes plain old java objects (POJO), there needs to be a easy way of creating POJOs.

SOLUTION

A Domain Object is a Spring DSL artifact that defines the domain model of the application. By default a domain object is generated into a plain old java object (POJO). If a domain object is associated with a data access object, then the domain object is annotated as an @Entity (JPA) annotated class, and it's associated with a primary key class (@IdClass). Fields can be added to domain objects by using the basic data types, and a domain object can have relationships to other domain objects.

HOW IT WORKS

All applications work with data, and domain objects specify the kind of data that can be stored and manipulated within a program. Skyway supports a number of fundamental basic data types (scalar data types), which are the simplest data types in a Skyway application.

The following table lists the basic data types supported.

Table 4.1. Skyway Basic Data Types

NameDescription
IDjava.lang.String
Textjava.lang.String
Integerjava.lang.Integer
Decimaljava.math.BigDecimal
Date and Timejava.util.Calendar
Booleanjava.lang.Boolean
Large Textjava.lang.String
Datejava.util.Calendar
Mapjava.util.HashMap
Timejava.util.Calendar
PictureJava Byte Array
Large Data StorageJava Byte Array

Skyway Domain Objects are used to define objects. As described in the Spring DSL model, domain objects can be created in model packages.

Steps for creating a Domain Object:

  1. Right click on a Model Package, and select New-->Domain Object to open the New Domain Object Wizard.

  2. From the New Domain Object panel, enter a name for the domain objectl. Click Finish to open the Domain Object Editor.

  3. From the Domain Object Editor add fields to the domain object. A field consists of a field name and field type. You can also specify whether the field is a collection and a primary key.

  4. You can also relate the current domain object to other domain objects by adding relationships.

This figure shows a domain object being defined in Skyway Builder. Attributes were added as fields. For each field you can specify the type (Text, Integer, etc..) and whether it's a collection. You can also specify which attribute(s) uniquely identify the object from other objects of the same type.

Figure 4.1. Creating a domain object using Domain Object Editor

Creating a domain object using Domain Object Editor

This is an example of the code that is generated from the Order domain object defined in the previous figure.

Example 4.1. Domain Object - POJO (Generated)

package domain.ordermgmt;

public class Order implements Serializable{

   /**
   * 
   * @generated
   */
   private String invoiceId;
   private Calendar invoiceDate;
   private Integer lineItemCount;

   // Constructors, Getters and Setters
   ...
}

For the fields that were defined as primary keys, Skyway Builder will generate primary key classes. Composite keys are supported.

Example 4.2. Domain Object - Primary Key Class (Generated)

package domain.ordermgmt;

public class OrderPK implements Serializable {

    /**
    * 
    * @generated
    */
    private String invoiceId;

   // Constructors, Getters and Setters, Hashcode() and Equals()
   ...
}

By default a field is scalar; it can store one value of the basic type that is specified. If you mark the field as a collection, the field becomes a composite field that can store one or more values of that domain object.