Play Framework 2 Tutorial: Ebean ORM
How to use the Ebean ORM with the Play framework
Published: 01 Jun 2012
In this post, part five of the tutorial series about the Play Framework, you’ll learn the basics of creating models and using the Ebean ORM.
Configuring Ebean
In order to start using Ebean in your project, you have to enable it. Add the following to conf/application.conf
:
ebean.default="models.*"
Creating a Model
Create app/models/Budget.java
and add the following:
package models;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;
@Entity
public class Budget extends Model {
@Id
public Long id;
@Constraints.Required
public String name;
@Formats.DateTime(pattern = "MM/dd/yy")
public Date createDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public static Finder<Long, Budget> find = new Finder<Long, Budget>(Long.class, Budget.class);
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Budget");
sb.append("{id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append('}');
return sb.toString();
}
}
Testing
Now let’s create a test for our new model:
package models;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebean.config.dbplatform.H2Platform;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.server.ddl.DdlGenerator;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import play.libs.Yaml;
import play.test.FakeApplication;
import play.test.Helpers;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import static org.fest.assertions.Assertions.assertThat;
public class BudgetTest {
public static FakeApplication app;
public static DdlGenerator ddl;
public static EbeanServer server;
@BeforeClass
public static void setup() {
app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
Helpers.start(app);
server = Ebean.getServer("default");
ServerConfig config = new ServerConfig();
config.setDebugSql(true);
ddl = new DdlGenerator((SpiEbeanServer) server, new H2Platform(), config);
}
@AfterClass
public static void stopApp() {
Helpers.stop(app);
}
@Before
public void resetDb() throws IOException {
// drop
String dropScript = ddl.generateDropDdl();
ddl.runScript(false, dropScript);
// create
String createScript = ddl.generateCreateDdl();
ddl.runScript(false, createScript);
// insert data
Map<String, List<Object>> all = (Map<String, List<Object>>) Yaml.load("initial-data.yml");
Ebean.save(all.get("budgets"));
}
@Test
public void findById() {
Budget b = Budget.find.ref(1L);
assertThat(b.id).isEqualTo(1L);
}
@Test
public void findAll() {
List<Budget> budgets = Budget.find.all();
assertThat(budgets).isNotNull();
assertThat(budgets.size()).isEqualTo(2);
}
@Test
public void insert() {
Long lastId = getLastId();
Budget budget = new Budget();
budget.name = "test budget";
budget.createDate = new Date();
budget.save();
Budget savedBudget = Budget.find.byId(lastId + 1);
assertThat(savedBudget.id).isNotNull();
assertThat(savedBudget.createDate).isNotNull();
assertThat(savedBudget.name).isNotNull();
assertThat(savedBudget.name).isEqualTo("test budget");
}
@Test
public void delete() {
Long lastId = getLastId();
int oldCount = Budget.find.all().size();
Budget existingBudget = Ebean.find(Budget.class, lastId);
Ebean.delete(existingBudget);
int currentCount = Budget.find.all().size();
assertThat(currentCount).isEqualTo(oldCount - 1);
}
@Test
public void updateSimple() {
Long lastId = getLastId();
Budget existingBudget = Ebean.find(Budget.class, lastId);
String newName = existingBudget.name + "foo";
existingBudget.setName(newName);
Ebean.update(existingBudget);
Budget updatedBudget = Ebean.find(Budget.class, lastId);
assertThat(updatedBudget.id).isEqualTo(lastId);
assertThat(updatedBudget.name).isEqualTo(newName);
}
@Test
public void updateViaSql() {
Long lastId = getLastId();
Budget existingBudget = Budget.find.byId(lastId);
String existingName = existingBudget.name;
String newName = existingName + "foo";
Ebean.createSqlUpdate("update budget set name = :newName where id = :id")
.setParameter("newName", newName)
.setParameter("id", lastId)
.execute();
Budget updatedBudget = Budget.find.byId(lastId);
assertThat(updatedBudget.id).isEqualTo(lastId);
assertThat(updatedBudget.name).isEqualTo(newName);
}
private Long getLastId() {
return (Long) Budget.find.orderBy("id desc").findIds().get(0);
}
}