-
Notifications
You must be signed in to change notification settings - Fork 13
Step 2 working with models
Purpose: show how to work with your favorite IDE, and JPA support in play.
Note: you can get the sources of this step with the following commands
cd ~/devel/apps
git clone git://github.com/opensas/play-demo.git
cd play-demo
git checkout origin/02-working_with_models
You can configure your play app to work with eclipse. Just go to the app's root folder, and type
cd ~/devel/apps/play-demo
play eclipsify
This will generate the eclipse project, so just start Eclipse, select File, Import, Existing Projects into Workspace, and just point to your app's root directory.
Play framework also comes with an eclipse plugin, just copy it from play's installation directory to eclipse dropins directory, like this:
cp <play dir>/support/eclipse/org.playframework.playclipse_0.7.0.jar <eclipse dir>/dropins/
Accordingly you can issue play netbeansify and play idealize to configure your app for netbeans and intelliJ respectively.
In eclipse, go to the app/models package, and select Play!, New Model from the main menu. In model name type EventType.
Open the created class (of course, you can also manually create it yourself) and add a name property.
package models;
import play.*;
import play.db.jpa.*;
import javax.persistence.*;
import java.util.*;
@Entity
public class EventType extends Model {
public String name;
}
Notice the @Entity annotation, it's a common mistabke to forget it.
Create also an EventModel, with the following info
@Entity
public class Event extends Model {
public String name;
@ManyToOne
public EventType type;
public String place;
public Date date;
}
Now we'll create and action in the Application controller that retrieves all the events. So you can delete previous index action, and create the following method
public static void list() {
List<Event> events = Event.find("order by date desc").fetch();
render(events);
}
in play, public static methods, returning void, in classes extending controllers, are said to be actions methods.
Now we'll modify the view to show the retrieves events. You can delete app/views/Application/index.html and replace it with the following app/views/Application/list.html
#{extends 'main.html' /}
#{set title:'Home' /}
#{if events}
<table border="1">
<thead>
<th>#</th>
<th>Event</th>
<th>Type</th>
<th>Place</th>
<th>Date</th>
<th></th>
</thead>
#{list events, as:'event'}
<tr>
<th>${event.id}</th>
<th>${event.name}</th>
<th>${event.type.name}</th>
<th>${event.place}</th>
<th>${event.date}</th>
<th></th>
</tr>
#{/list}
</table>
#{/if}
#{else}
¡No events in sight!
#{/else}
Finally we have to map incoming request to our newly created list action. So open conf/routes file and add the following
GET / Application.list
GET /events Application.list
Now go to localhost:9000.
JPA error
A JPA error occurred (Cannot start a JPA manager without a properly configured database): No datasource configured
Play tells us that we forgot to configure our database. Just open conf/application.conf and uncomment the followgin line:
[...]
# To quickly set up a development database, use either:
# - mem : for a transient in memory database (H2 in memory)
# - fs : for a simple file written database (H2 file stored)
db=mem
#
# To connect to a local MySQL5 database, use:
[...]
No go back to your browser and refresh the page.
Everything seems to work, but we have no data. Go to Step 3 - populating data to see how we can add some events to our database.