Changes for page Your First Rest Project
Last modified by Steve Peery on 2013/09/06 11:02
From version 2.1
edited by Pascal Robert
on 2011/12/27 07:16
on 2011/12/27 07:16
Change comment:
There is no comment for this version
To version 5.1
edited by Pascal Robert
on 2011/12/27 12:48
on 2011/12/27 12:48
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,4 +1,93 @@ 1 -* Database (H2, ready out of the box with Wonder) 2 -* Create Your Model (Person and BlogEntry) 3 -* Implement Migrations 4 -* Rest 1 +{{toc}}{{/toc}} 2 + 3 += Introduction = 4 + 5 +In the first part of the Blog tutorial, you will learn: 6 + 7 +* How to create a EOModel for the database (we will use H2) 8 +* How to use migrations to create the database tables 9 +* How to use ERRest to create blog posts with JSON format and how to display the blog posts in HTML for readers 10 + 11 += Create a new project = 12 + 13 +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//. 14 + 15 += Create the database model = 16 + 17 +== Database structure == 18 + 19 +We will build a small database model for the blog. The database will have two tables: BlogEntry and Author. 20 + 21 +BlogEntry will have the following columns: 22 + 23 +|= Column name |= Type |= Constraints 24 +| id | integer | primary key 25 +| title | string(255) | 26 +| content | string(4000) | 27 +| lastModified | timestamp | 28 +| creationDate | timestamp | 29 +| author | integer | relation with Author 30 + 31 +Author will have the following columns: 32 + 33 +|= Column name |= Type |= Constraints 34 +| id | integer | primary key 35 +| firstName | string(50) | 36 +| lastName | string(50) | 37 +| email | string(100) | unique 38 +| passwd | string(16) | 39 + 40 +== Creating the EOModel == 41 + 42 +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). 43 + 44 +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. 45 + 46 +To create the EOModel, in the project right-click on the project name and select **New** -> **EOModel**. 47 + 48 +Name it **BlogModel** and in the plugin list, select **H2**. Click **Finish**. 49 + 50 +The model should show up in a window that looks like this: 51 + 52 +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. 53 + 54 +In the Entity Modeler window, click on **Default**, and for the **URL** field, type {{code}}jdbc:h2:~/BlogTutorial{{/code}}. When the database will be created, it will be stored in your home directory (/Users/youruser/ on OS X). 55 + 56 +Now, right-click on **BlogModel** and select **New Entity**. 57 + 58 +Type the following details in the **Basic** tab: 59 + 60 +* **Name**: BlogEntry 61 +* **Table Name**: BlogEntry 62 +* **Class Name**: your.app.model.BlogEntry 63 + 64 +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. 65 + 66 +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: 67 + 68 +* **Name**: title 69 +* **Column**: title 70 +* **Prototype**: varchar255 71 + 72 +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. 73 + 74 +Now, repeat the last two steps to create the other attributes for the **BlogEntry** entity, with the following values: 75 + 76 +|=Attribute name|=Column|=Prototype 77 +| content | content | longtext 78 +| lastModified | lastModified | dateTime 79 +| creationDate | creationDate | dateTime 80 + 81 +If you did everything well, the list of attributes should look like this: 82 + 83 +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: 84 + 85 +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: 86 + 87 +|=Attribute name|=Column|=Prototype 88 +| firstName | firstName | varchar50 89 +| lastName | lastName | varchar50 90 +| email | email | varchar100 91 +| passwd | passwd | varchar16 92 + 93 +Final list of attributes should look like this: