Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

What we are going to do is to write a simple method that returns the full name of an author, e.g. a method that simply concatenate the first name, a space and the last name of the author. To do so, double-click on Author.java and add the following methods:

Code Block
	public String fullName() {
	  return this.firstName() + " " + this.lastName();
	}

Nothing fancy here. And you are now ready to create REST controllers. Now, let's use migrations to actually create the database.

Using migrations

Migrations allow you to create the tables and columns (and some types of constraint). Entity Modeler have support to generate the code for the first migration, which is called "migration 0". To do that, open the EOModel (BlogModel EOModel in the Resources folder), right-click on the model name and select Generate Migration.

Copy the generated code in the clipboard. Close Entity Modeler and in the main Eclipse window, right-click on Sources, select New and select Class.

Type your.app.model.migrations as the package and BlogModel0 as the name of the class. Click Finish.

In the Sources folder, open the your.app.model.migrations package, a class named BlogModel0 should be there. Delete everything in that file EXCEPT the first line (which should be package your.app.model.migrations) and paste the code that was generated by Entity Modeler. Save the file.

One last step: migrations are disabled by default. To enable them, you need to uncomment two properties in the Properties file that is located in the Resources folder. Open that file (double-click on it).

Remove the pound char in front of those two properties:

Code Block

#er.migration.migrateAtStartup=true
#er.migration.createTablesIfNecessary=true

After removing the pound char, the two properties should look like this:

Code Block

er.migration.migrateAtStartup=true
er.migration.createTablesIfNecessary=true

You are now ready to start the application so that it creates the database! To do so, right-click on Application.java (in the your.app folder) and select Run As -> WOApplication. In Eclipse's Console tab, you should see some output, including something similar to:

Code Block

BlogRest[62990] INFO  er.extensions.migration.ERXMigrator  - Upgrading BlogModel to version 0 with migration 'your.app.model.migrations.BlogModel0@4743bf3d'
BlogRest[62990] INFO  er.extensions.jdbc.ERXJDBCUtilities  - Executing CREATE TABLE Author(email VARCHAR(100) NOT NULL, firstName VARCHAR(50) NOT NULL, id INTEGER NOT NULL, lastName VARCHAR(50) NOT NULL, passwd VARCHAR(16) NOT NULL)
BlogRest[62990] INFO  er.extensions.jdbc.ERXJDBCUtilities  - Executing ALTER TABLE Author ADD PRIMARY KEY (id)
BlogRest[62990] INFO  er.extensions.jdbc.ERXJDBCUtilities  - Executing CREATE TABLE BlogEntry(authorID INTEGER NOT NULL, content TIMESTAMP NOT NULL, creationDate TIMESTAMP NOT NULL, id INTEGER NOT NULL, lastModified TIMESTAMP NOT NULL, title VARCHAR(255) NOT NULL)
BlogRest[62990] INFO  er.extensions.jdbc.ERXJDBCUtilities  - Executing ALTER TABLE BlogEntry ADD PRIMARY KEY (id)
BlogRest[62990] INFO  er.extensions.jdbc.ERXJDBCUtilities  - Executing ALTER TABLE BlogEntry ADD CONSTRAINT "FOREIGN_KEY_BLOGENTRY_AUTHORID_AUTHOR_ID" FOREIGN KEY (authorID) REFERENCES Author (id)
BlogRest[62990] DEBUG NSLog  -  evaluateExpression: <er.h2.jdbcadaptor.ERH2PlugIn$H2Expression: "UPDATE _dbupdater SET version = ? WHERE modelname = ?" withBindings: 1:0(version), 2:"BlogModel"(modelName)>

If you see this and that the application is running (it should open a window in your favorite browser), migration worked and your database have been created, congratulations! You can now stop the application (click the square red button in Eclipse's Console tab) and continue to the next step.

Creating REST controllers and routes