1. Ebean

Persistence module using Ebean: https://ebean.io

1.1. Usage

1) Add the dependencies (hikari + ebean):

Maven
Gradle
<!-- DataSource via HikariCP-->
<dependency>
  <groupId>io.jooby</groupId>
  <artifactId>jooby-hikari</artifactId>
  <version>3.5.3</version>
</dependency>

<!-- Ebean Module-->
<dependency>
  <groupId>io.jooby</groupId>
  <artifactId>jooby-ebean</artifactId>
  <version>3.5.3</version>
</dependency>

2) Add database driver (mySQL here):

Maven
Gradle
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>${mysql-connector-java.version}</version>
</dependency>

3) Set database properties

application.conf
db.url = "jdbc:mysql://localhost/mydb"
db.user = myuser
db.password = mypass

4) Configure build time enhancement of the entity beans

Maven
Gradle
<plugin>
  <groupId>io.repaint.maven</groupId>
  <artifactId>tiles-maven-plugin</artifactId>
  <version>${tiles-maven-plugin.version}</version>
  <extensions>true</extensions>
  <configuration>
    <tiles>
      <!-- other tiles ... -->
      <tile>io.ebean.tile:enhancement:15.8.0</tile>
    </tiles>
  </configuration>
</plugin>

4) Install and use Ebean

Java
Kotlin
import io.jooby.hikari.HikariModule;
import io.jooby.ebean.EbeanModule;

{
  install(new HikariModule());                     (1)

  install(new EbeanModule());                      (2)

  get("/", ctx -> {
    Database db = require(Database.class);         (3)
    // work with Database
  });
}
1 Install and creates a DataSource
2 Install Ebean
3 Use Ebean Database

1.2. Transactional Request

The TransactionalRequest decorator takes care of a start/commit/rollback a transaction per HTTP request.

TransactionalRequest
Java
Kotlin
import io.jooby.hikari.HikariModule;
import io.jooby.ebean.EbeanModule;
import io.jooby.ebean.TransactionalRequest;

{
  install(new HikariModule());

  install(new HibernateModule());

  use(new TransactionalRequest());

  post("/create", ctx -> {
    Database db = require(Database.class);

    MyEntity e = ...;

    db.save(e);

    return e;
  });
}

1.2.1. @Transactional

If you simply install the decorator it becomes enabled by default, this means that each route in its scope become transactional. You can exclude an MVC route by annotating it with the Transactional annotation:

@Transactional
Java
Kotlin
import io.jooby.annotation.Transactional;

@Transactional(false)
@GET("/")
public void get(Context ctx) {
  // no automatic transaction management here
}

You also have the option to invert this logic by disabling the decorator by default:

TransactionalRequest disabled by default
Java
Kotlin
import io.jooby.ebean.TransactionalRequest;

{
  ...
  use(new TransactionalRequest().enabledByDefault(false));
  ...
}

Then you can enable it for the selected routes using @Transactional(true):

@Transactional
Java
Kotlin
import io.jooby.annotation.Transactional;

@Inject
private Database database;

@Transactional(true)
@GET("/")
public void get(Context ctx) {
  // work with Database
}

This feature is not limited to MVC routes. For script routes use the constant Transactional.ATTRIBUTE:

Transactional for script routes
Java
Kotlin
{
  get("/", ctx -> {
    ...
  }).attribute(Transactional.ATTRIBUTE, false);
}

1.3. Configuration

Advanced/Custom configuration is supported programmatically or using property files.

Programmatically
Java
Kotlin
{
  DatabaseConfig dbConfig = ...;         (1)
  install(new EbeanModule(dbConfig));    (2)
}
1 Manually creates a database config or use the one provided by Jooby: create(Jooby,String).
2 Install Ebean with custom database config
Configuration
{
  ebean {
    ddl {
      generate = true
      run = true
    }
  }
}

Example shows how to setup Ebean migration tools. Keep in mind Jooby offers a better solution for database migrations Flyway Module.