Skip to content

Defining Data Model and Creating the Database

Olga Shiryaeva edited this page Dec 5, 2017 · 2 revisions

According to the ER diagram, we will create 4 entities: Client, Mechanic, SparePart and Order and one enumeration OrderStatus.

Data model

3.1 Client Entity

  1. Open the DATA MODEL section of the left-hand side navigation panel and click New -> Entity

  2. Set Class name: Client

  3. Click OK

Create Client entity

  1. Click the New button under the Attributes table

  2. Enter Name: name

  3. Select the Mandatory checkbox

  4. Click Add

Create name attribute

  1. Repeat steps 4-7 to create phoneNumber as a mandatory string attribute with the length of 20 and flagged as unique

  2. Repeat steps 4-7 to create email as a mandatory string attribute with the length of 50 and flagged as unique

  3. Go to the INSTANCE NAME tab of the entity designer screen. On this tab you can specify the default string representation of an entity to be shown in tables, dropdowns, etc.

  4. Select name and phoneNumber fields one by one

Client instance name

  1. Clicking the SOURCE and DDL PREVIEW tabs you can find your Entity bean implementation annotated with the javax.persistence annotations and SQL script for creating the corresponding table

  2. Click OK in the right-top corner of the entity designer screen to save the entity

3.2 Mechanic Entity

  1. Create a New entity with Class name: Mechanic and click OK

  2. Click the New button under the Attributes table.

Mechanic will be a system user, let’s add the user attribute with a link to the User entity. The User entity is a standard entity storing system users in the CUBA Platform.

  1. Fill up the Create attribute form as following and click OK
  • Name: user
  • Attribute type: Association
  • Type: User [sec$User]
  • Cardinality: MANY_TO_ONE
  • Mandatory: Yes

Create user attribute

Studio has generated the name of the association column in the DB automatically.

  1. Create one more attribute. For mechanics we’ll need one more mandatory attribute Hourly Rate.

Apply the following settings and click Add:

  • Name: hourlyRate
  • Type: BigDecimal
  • Mandatory: Yes
  1. Move to the INSTANCE NAME tab and select user as an instance name for Mechanic

  2. Check that your Mechanic entity corresponds to the picture below and click OK

Mechanic entity

3.3 SparePart Entity

  1. Create a New entity with Class name: SparePart

  2. Add an attribute

  • Name: title
  • Type: String
  • Mandatory: Yes
  • Unique: Yes
  1. Add an attribute
  • Name: description
  • Type: String
  • Length: Unlimited
  1. Add an attribute
  • Name: price
  • Type: BigDecimal
  • Mandatory: Yes
  1. Go to the INSTANCE NAME tab and select the title attribute as the entity instance name

  2. Check that that your SparePart entity corresponds to the picture below and click OK

SparePart entity

3.4 OrderStatus Enum

For the Order entity, we’ll need to create the OrderStatus enum first. The status will be stored in the DB as numeric values.

  1. Open the DATA MODEL section of the left-hand side navigation panel and click New -> Enumeration.

  2. Enter Class name: OrderStatus

  3. Add values in the uppercase according to the Java naming convention:

  • NEW 10
  • IN_PROGRESS 20
  • READY 30
  1. Check that your OrderStatus enum corresponds to the picture below and click OK. Similar to entities, we can check the generated Java code in the Source tab.

OrderStatus enum

3.5 Order Entity

The last missing entity of our data model is Order.

  1. Create a New entity with Class name: Order

  2. To add a link to the client, add an attribute which is a relation to the Client entity.

  • Name: client
  • Attribute type: ASSOCIATION
  • Type: Client
  • Cardinality: MANY_TO_ONE
  • Mandatory: Yes
  1. Then add a link to the mechanic that performs the order. Add an attribute:
  • Name: mechanic
  • Attribute type: ASSOCIATION
  • Type: Mechanic
  • Cardinality: MANY_TO_ONE
  • Mandatory: Yes
  1. Add the order description, this will be an unlimited length string:
  • Name: description
  • Type: String
  • Length: Unlimited
  1. Similarly, add the hoursSpent attribute:
  • Name: hoursSpent
  • Type: Integer
  1. And the amount attribute
  • Name: amount
  • Type: BigDecimal
  1. Now we need to link our order with the SparePart list. Add an attribute
  • Name: parts
  • Attribute type: ASSOCIATION
  • Type: SparePart
  • Cardinality: MANY_TO_MANY

The Studio will offer to create an inverse attribute - click No

  1. The last attribute will be status. Add the attribute, in the creation dialogue, specify the ENUM type and set mandatory.
  • Name: status
  • Attribute type: Enum
  • Type: OrderStatus
  1. Set the INSTANCE NAME for the Order entity to its description attribute

  2. Check that that your Order entity corresponds to the picture below and click OK

Order entity

3.6 Generate DB Scripts and Create the Database

  1. Click the Generate DB scripts at the bottom of the DATA MODEL section; the CUBA Studio will generate a script to create tables and constraints

  2. Click Save and close and the Studio will save the scripts into a special directory of our project, so we will be able to access them if needed

DB scripts

  1. Invoke the Run — Create database action from the menu to create a database

Run - Create database

  1. The CUBA Studio warns us that the old DB will be deleted, click OK

next →