opensas edited this page Apr 3, 2012 · 2 revisions

Using the Ebean ORM

Esta página todavía no ha sido traducida al castellano. Puedes ayudarnos con la tarea simplemente presionando el botón Edit Page. Para más información puedes leer esta guía para el traductor. Aquí puedes ver cuánto nos falta para terminar la traducción.

Configuring Ebean

Play 2.0 comes with the Ebean ORM. To enable it, add the following line to conf/application.conf:


This defines a default Ebean server, using the default data source, which must be properly configured. You can actually create as many Ebean servers you need, and explicitly define the mapped class for each server.


In this example, we have access to two Ebean servers - each using its own database.

For more information about Ebean, see the Ebean documentation.

Using the play.db.ebean.Model superclass

Play 2.0 defines a convenient superclass for your Ebean model classes. Here is a typical Ebean class, mapped in Play 2.0:

package models;

import java.util.*;
import javax.persistence.*;

import play.db.ebean.*;

public class Task extends Model {

  public Long id;
  public String name;
  public boolean done;
  public Date dueDate = new Date();
  public static Finder<Long,Task> find = new Finder<Long,Task>(
    Long.class, Task.class


As you can see, we've added a find static field, defining a Finder for an entity of type Task with a Long identifier. This helper field is then used to simplify querying our model:

// Find all tasks
List<Task> tasks = Task.find.all();
// Find a task by ID
Task anyTask = Task.find.byId(34L);

// Delete a task by ID

// More complex task query
List<Task> tasks = find.where()
    .ilike("name", "%coco%")
    .orderBy("dueDate asc")

Transactional actions

By default Ebean will not use transactions. However, you can use any transaction helper provided by Ebean to create a transaction. For example:

// run in Transactional scope...  
Ebean.execute(new TxRunnable() {  
  public void run() {  
    // code running in "REQUIRED" transactional scope  
    // ... as "REQUIRED" is the default TxType  
    // find stuff...  
    User user = Ebean.find(User.class, 1);  
    // save and delete stuff...;  

You can also annotate your action method with @play.db.ebean.Transactional to compose your action method with an Action that will automatically manage a transaction:

public static Result save() {

Next: Integrating with JPA

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.