Find file
Fetching contributors…
Cannot retrieve contributors at this time
190 lines (141 sloc) 4.14 KB

Access SQL Database with Ebean


In order to use Ebean you need to add the following dependencies in your pom.xml file:


You will also need to import your JDBC or database packages something like



# If you have only one DBPlugin in your class path, then
# you do not need to specify the db.impl configuration
# database driver default to org.h2.Driver
# database Url default to jdbc:h2:mem:tests
# username default is empty
# password default is empty
# If specified then app scan package will be used instead


Let's create a simple Contact model with three properties:

  1. firstName
  2. lastName
  3. address
package com.mycom.myprj;

import act.db.DB;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name = "ctct")
public class Contact {
    private Long id;
    private String fn;
    private String ln;
    private String addr;

    public long getId() {
        return null == id ? -1 : id;

    public String getFirstName() {
        return fn;

    public void setFirstName(String fn) {
        this.fn = fn;

    public String getLastName() {
        return ln;

    public void setLastName(String ln) {
        this.ln = ln;

    public String getAddress() {
        return addr;

    public void setAddress(String addr) {
        this.addr = addr;

Note unlike Morphia which allows you to extend your Model class to MorphiaModel, Ebean plugin does not support that at the moment.


Now that the model has been defined, let's take a look at how to use built-in DAO to do CRUD operations.

package com.mycom.myprj;

import act.controller.Controller;
import act.db.ebean.EbeanDao;
import org.osgl.$;
import org.osgl.mvc.annotation.DeleteAction;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.PutAction;

import javax.inject.Inject;

public class ContactController extends Controller.Util {

    private EbeanDao<Long, Contact> dao;

    public ContactController(EbeanDao<Long, Contact> dao) {
        this.dao = dao;

    public Iterable<Contact> list() {
        return dao.findAll();

    public void create(Contact ctct) {;

    public Contact show(long id) {
        return dao.findById(id);

    public void updateAddress(long id, String value) {
        Contact ctct = dao.findById(id);

    public void delete(long id) {


Note the different between Morphia plugin and Ebean plugin on how to get the Dao instance.


Act provide a set of search methods in Dao interface:

// find by last name
Iterable<Contact> contacts = dao.findBy("firstName", firstName);

// find by both first and last name
Iterable<Product> contacts = dao.findBy("firstName,lastName", firstName, lastName);

// find by firstName using regular expression
Iterable<Product> contacts = dao.findBy("firstName", Pattern.compile(firstName));

Extend EbeanDao


Using extended DAO class

Once you have extended the DAO, you can use it following the same way as shown above, just change the type as shown below:

//private EbeanDao<Contact> dao = $.cast(app.dbServiceManager().dao(Contact.class));
private Contact.Dao dao = $.cast(app.dbServiceManager().dao(Contact.class));

So ActFramework detects your implementation of the DAO and will use that class instead of the standard one.

Back to index