Your First Rest Project
Introduction
In the first part of the Blog tutorial, you will learn:
- How to create a EOModel for the database (we will use H2)
- How to use migrations to create the database tables
- How to use ERRest to create blog posts with JSON format and how to display the blog posts in HTML for readers
Create a new project
You will need to create a new project for this tutorial. In Eclipse, open the File menu, select New and select Wonder REST Application. Name your project as BlogRest.
Create the database model
Database structure
We will build a small database model for the blog. The database will have two tables: BlogEntry and Author.
BlogEntry will have the following columns:
Column name | Type | Constraints |
---|---|---|
id | integer | primary key |
title | string(255) | |
content | string(4000) | |
lastModified | timestamp | |
creationDate | timestamp | |
author | integer | relation with Author |
Author will have the following columns:
Column name | Type | Constraints |
---|---|---|
id | integer | primary key |
firstName | string(50) | |
lastName | string(50) | |
string(100) | unique | |
passwd | string(16) |
Creating the EOModel
To create the database, we will first create a EOModel and use migrations to build the database on the file system (H2 will take care of creating the database file).
An EOModel consists of entities, attributes and relationships. When using it in a RDBMS context, an entity is a table (or a view), an attribute is a table column and a relationship is a join between two tables.
To create the EOModel, in the project right-click on the project name and select New -> EOModel.
Name it BlogModel and in the plugin list, select H2. Click Finish.
The model should show up in a window that looks like this:
If it didn't show up, the window might have opened behind the main Eclipse window. If that's the case, open the Window menu and select the windows that have Entity Modeler in its name.
In the Entity Modeler window, click on Default, and for the URL field, type jdbc:h2:~/BlogTutorial. When the database will be created, it will be stored in your home directory (/Users/youruser/ on OS X).
Now, right-click on BlogModel and select New Entity.
Type the following details in the Basic tab:
- Name: BlogEntry
- Table Name: BlogEntry
- Class Name: your.app.model.BlogEntry
Now, it's time to add the entity's attributes (aka, the table's columns). You will see that the entity already have an attributed named "id". That attribute is a integer for the primary key. Leave it there.
Let's create the first attribute: the title of the blog entry. Right-click on the entity and select New Attribute. Type the following values:
- Name: title
- Column: title
- Prototype: varchar255
When you use prototypes, you don't need to define the type (varchar, int, etc.) for the database, so by using prototypes, if you switch from a RDBMS system to another one, say from H2 to MySQL, you only need to change the JDBC connection string and bundle the EOF plugin for the RDBMS, no need to switch data types in the model.
Now, repeat the last two steps to create the other attributes for the BlogEntry entity, with the following values:
Attribute name | Column | Prototype |
---|---|---|
content | content | longtext |
lastModified | lastModified | dateTime |
creationDate | creationDate | dateTime |
If you did everything well, the list of attributes should look like this:
You will notice that the attributes have a column with a lock in it. When a lock is present, it will use the value of that attribute for UPDATE ... WHERE attribute = '' statement. This is to do optimistic locking, aka to prevent data conflict when the data object was modified by two different users. Using timestamps for optimistic locking is not a good idea because for certain RDBMS, the value can be different because of milliseconds, so remove the locks on the lastModified and creationDate attributes. The final list should look like this:
Next step is to create the Author entity. Create a new entity with Author at its name (and also as the table name), and for the class name, use your.app.model.Author. The attributes for this entity are:
Attribute name | Column | Prototype |
---|---|---|
firstName | firstName | varchar50 |
lastName | lastName | varchar50 |
varchar100 | ||
passwd | passwd | varchar16 |
Final list of attributes should look like this: