2.2. Creating the Data Model

Let us create an EBook entity class that will represent the electronic version of the book publication. The Ebook class must be derived from the Card (process card) in order to manage the EBook state in accordance with the required business process.

Go to the Entities tab on the Studio navigation panel, select the com.sample.library.entity package and click New entity. Specify the following entity properties:

  • Class name - EBook

  • Table - LIBRARY_EBOOK

  • Parent class - Card [wf$Card]. The InheritanceType.JOINED inheritance strategy is defined for the Card class, thus EBook will be stored in a separate table, and its primary key will be an external key that references the Card primary key.

  • Discriminator - 10. A discriminator is the value of a base type field that will be set in the database for all instances of this specific type. In this case, the following annotations have been defined for the Card base class:

    @DiscriminatorColumn(name = "CARD_TYPE", discriminatorType = DiscriminatorType.INTEGER)
    @DiscriminatorValue("0")

    This means that the CARD_TYPE field must store an integer value, which will be equal to 0 for base type instances. Therefore, it is sufficient to assign any value other than 0 for the EBook type.

  • In the Primary key join column field, the CARD_ID name for the primary key of the created entity is automatically generated.

  • In the Referenced primary key column field, the primary key for the WF_CARD table that stores the base Card entity is selected. In this case, it is ID.

Next, let us create an entity attribute containing a link to the book publication.

Click New under the list of attributes and specify the following properties in the Create attribute window:

  • Name - publication

  • Attribute type - ASSOCIATION

  • Type - BookPublication [library$BookPublication]

  • Mandatory - on. This means the attribute is required.

  • Cardinality - MANY_TO_ONE. This means that several EBook instances can be created for one BookPublication instance.

  • The Column field will contain the appropriate column name - PUBLICATION_ID.

Figure 2.1. Entity Editor

Entity Editor

Save the changes. As a result, the following entity class will be created:

package com.sample.library.entity;

import javax.persistence.*;

import com.haulmont.workflow.core.entity.Card;

@PrimaryKeyJoinColumn(name = "CARD_ID", referencedColumnName = "ID")
@DiscriminatorValue("10")
@Table(name = "LIBRARY_EBOOK")
@Entity(name = "library$EBook")
public class EBook extends Card {
    private static final long serialVersionUID = -7326357893869004530L;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "PUBLICATION_ID")
    protected BookPublication publication;

    public void setPublication(BookPublication publication) {
        this.publication = publication;
    }

    public BookPublication getPublication() {
        return publication;
    }
}

After the entity class is created, the Studio will report that the data model has been changed and now differs from the current database schema. It is necessary to generate the scripts and to start the database creation or update process.

Click Generate DB scripts in the Entites section of the navigation panel. The Studio will generate the database update and initialization scripts, which include the creation of the LIBRARY_EBOOK table and its foreign keys. Since our database contains no data and can be easily recreated, you can safely delete the scripts in the Update scripts tab. After that, save the changes.

Choose Run -> Stop application server to stop the application server. The Run -> Create database menu item, which should be used, will become available in a few seconds.