1. Introduction

The Ebean plugin enables lightweight access to datasources using Ebean ORM. This plugin does NOT provide domain classes nor dynamic finders like GORM does.

Griffon version: 2.12.0

2. Usage

The following sections describe how you may use this plugin in a project.

2.1. Configuration

This plugin relies on the griffon-datasource-plugin. Please follow the instructions to configure this plugin first.

You must create a configuration file named Ebean that holds the settings for creating instances of com.avaje.ebean.EbeanServer. This file follows the same standard configuration mechanism as the application’s Config file, which means you can define the configuration using

  • a properties file

  • a Java file

  • a Groovy script

The following example shows the default settings needed to connect the default database taking into account that each environment may connect to a different database.

src/main/resources/Ebean.groovy
ebeanServer {
    // specify any properties from com.avaje.ebean.config.ServerConfig
}

You may configure multiple named EbeanServers (the default server is aptly named default) as the following snippet shows

src/main/resources/Ebean.groovy
ebeanServers {
    internal {

    }
    people {

    }
}

environments {
    development {
        ebeanServer {
            // someConfigurationProperty = someValue
        }
    }
    test {
        ebeanServer {
            // someConfigurationProperty = someValue
        }
    }
    production {
        ebeanServer {
            // someConfigurationProperty = someValue
        }
    }
}

2.1.1. Ebean Properties

The following table summarizes the properties that can be specified inside a ebeanServer block

Property Type Default Description

schema

String

create

connect_on_startup

boolean

false

Establishes a connection to the datasource at the beginning of the Startup phase.

2.1.2. Accessing the Datasource

The plugin’s module registers a EbeanHandler helper class that defines the base contract for accessing a datasource and issue SQL queries to it. This class has the following methods

griffon.plugins.ebean.EbeanServerHandler.java
@Nullable
<R> R withEbean(@Nonnull EbeanServerCallback<R> callback)
    throws RuntimeEbeanServerException;

@Nullable
<R> R withEbean(@Nonnull String ebeanServerName, @Nonnull EbeanServerCallback<R> callback)
    throws RuntimeEbeanServerException;

void closeEbean();

void closeEbean(@Nonnull String ebeanServerName);

These method are aware of multiple datasources. If no ebeanServerName is specified when calling them then the default datasource will be selected. You can inject an instance of this class anywhere it’s needed using @Inject. There is one callback you may use with this method: EbeanServerCallback.

This callback is defined using a functional interface approach, which means you can apply lambda expressions if running with JDK8+ or closures if running Groovy.

griffon.plugins.ebean.EbeanServerCallback.java
public interface EbeanServerCallback<R> {
    R handle(@Nonnull String ebeanServerName, @Nonnull EbeanServer ebeanServer);
}

2.1.3. Entities

Ebean can discover all entities as long as they are annotated with @javax.persistence.Entity.

src/main/groovy/com/acme/Person.groovy
package griffon.plugins.ebean

import groovy.transform.ToString

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

@ToString
@Entity
class Person {
    @Id
    int id
    String name
    String lastname

    Map asMap() {
        [
            id      : id,
            name    : name,
            lastname: lastname
        ]
    }
}

All entities must be enhanced before usage. Ebean can register a java agent at runtime. this agent must be loaded before the application starts. You may use the application’s launcher for this task.

AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent", "debug=1");

2.1.4. Bootstrap

You may execute arbitrary database calls during connection and disconnection from a {com.avaje.ebean.EbeanServer}. Simply create a class that implements the EbeanServerBootstrap interface and register it within a module, for example

src/main/java/com/acme/SampleEbeanBootstrap.java
package com.acme;

import griffon.plugins.ebean.EbeanBootstrap;
import com.avaje.ebean.EbeanServer;

import javax.annotation.Nonnull;
import javax.inject.Named;

@Named("sample")
public class SampleEbeanBootstrap implements EbeanBootstrap {
    @Override
    public void init(@Nonnull String ebeanServerName, @Nonnull EbeanServer ebeanServer) {
        // operations after first connection to datasource
    }

    @Override
    public void destroy(@Nonnull String ebeanServerName, @Nonnull EbeanServer ebeanServer) {
        // operations before disconnecting from the datasource
    }
}
src/main/java/com/acme/ApplicationModule.java
package com.acme;

import griffon.plugins.ebean.EbeanBootstrap;
import griffon.core.injection.Module;
import org.codehaus.griffon.runtime.core.injection.AbstractModule;
import org.kordamp.jipsy.ServiceProviderFor;

@ServiceProviderFor(Module.class)
public class ApplicationModule extends AbstractModule {
    @Override
    protected void doConfigure() {
        bind(EbeanBootstrap.class)
            .to(SampleEbeanBootstrap.class)
            .asSingleton();
    }
}

2.2. Example

The following is a trivial usage of the EbeanHandler inside a Java service

com.acme.SampleService.java
package com.acme;

import griffon.core.artifact.GriffonService;
import griffon.metadata.ArtifactProviderFor;
import org.codehaus.griffon.runtime.core.artifact.AbstractGriffonService;

import griffon.plugins.ebean.EbeanHandler;
import griffon.plugins.ebean.EbeanCallback;
import org.ebean.Ebean;

import javax.annotation.Nonnull;
import javax.inject.Inject;

@ArtifactProviderFor(GriffonService.class)
public class SampleService extends AbstractGriffonService {
    @Inject
    private EbeanHandler ebeanHandler;

    public String getPersonName(final int id) {
         return ebeanHandler.withEbean(new EbeanCallback<String>() {
             public String handle(@Nonnull String ebeanServerName, @Nonnull Ebean ebean) {
                 Person person =  ebean.find(Person.class).setId(id).findUnique();
                 return person != null ? person.getName() : null;
         });
    }
}

Here’s the Groovy version of it

com.acme.SampleService.groovy
package com.acme

import griffon.core.artifact.GriffonService
import griffon.metadata.ArtifactProviderFor

import griffon.plugins.ebean.EbeanHandler
import com.avaje.ebean.EbeanServer

import javax.inject.Inject

@ArtifactProviderFor(GriffonService)
class SampleService {
    @Inject
    private EbeanHandler ebeanHandler

    String getPersonName(int id) {
         ebeanHandler.withEbean { String ebeanServerName, EbeanServer ebeanServer ->
             ebeanServer.find(Person).setId(id).findUnique()?.name
         }
    }
}

2.3. Events

The following events will be triggered by EbeanHandler

EbeanConnectStart(String ebeanServerName, Map<String, Object> config)

Triggered before connecting to the datasource.

EbeanConnectEnd(String ebeanServerName, Map<String, Object> config, EbeanServer ebenServer)

Triggered after connecting to the datasource.

EbeanDisconnectStart(String ebeanServerName, Map<String, Object> config, EbeanServer ebenServer)

Triggered before disconnecting from the datasource.

EbeanDisconnectEnd(String ebeanServerName, Map<String, Object> config)

Triggered after disconnecting from the datasource.

DataSource events may be triggered during connection and disconnection from a {com.avaje.ebean.EbeanServer}.

2.4. AST Transformation

You can apply the @EbeanAware AST transformation on any class. This injects the behavior of EbeanHandler into said class. The previous Groovy service example can be rewritten as follows

com.acme.SampleService.groovy
package com.acme

import griffon.core.artifact.GriffonService
import griffon.metadata.ArtifactProviderFor
import griffon.transform.EbeanAware

import com.avaje.ebean.EbeanServer

@EbeanAware
@ArtifactProviderFor(GriffonService)
class SampleService {
    String getPersonName(int id) {
         withEbean { String ebeanServerName, EbeanServer ebeanServer ->
              ebeanServer.find(Person).setId(id).findUnique()?.name
         }
    }
}

2.5. DSL Descriptors

This plugin provides DSL descriptors for Intellij IDEA and Eclipse (provided you have the Groovy Eclipse plugin installed). These descriptors are found inside the griffon-ebean-groovy-compile-2.1.0.jar, with locations

  • dsdl/griffon_ebean.dsld

  • gdsl/griffon_ebean.gdsl

3. Build Configuration

3.1. Gradle

You have two options for configuring this plugin: automatic and manual.

3.1.1. Automatic

As long as the project has the org.codehaus.griffon.griffon plugin applied to it you may include the following snippet in build.gradle

dependencies {
    griffon 'org.codehaus.griffon.plugins:griffon-ebean-plugin:2.1.0'
}

The griffon plugin will take care of the rest given its configuration.

3.1.2. Manual

You will need to configure any of the following blocks depending on your setup

dependencies {
    compile 'org.codehaus.griffon.plugins:griffon-ebean-core:2.1.0'
}
Compile Only
dependencies {
    compileOnly 'org.codehaus.griffon.plugins:griffon-ebean-groovy-compile:2.1.0'
}

3.2. Maven

First configure the griffon-ebean-plugin BOM in your POM file, by placing the following snippet before the <build> element

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.griffon.plugins</groupId>
            <artifactId>griffon-ebean-plugin</artifactId>
            <version>2.1.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Next configure dependencies as required by your particular setup

<dependency>
    <groupId>org.codehaus.griffon.plugins</groupId>
    <artifactId>griffon-ebean-core</artifactId>
</dependency>
Provided scope
<dependency>
    <groupId>org.codehaus.griffon.plugins</groupId>
    <artifactId>griffon-ebean-groovy-compile</artifactId>
</dependency>

Don’t forget to configure all -compile dependencies with the maven-surefire-plugin, like so

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <classpathDependencyExcludes>
            <classpathDependencyExclude>
                org.codehaus.griffon:griffon-ebean-groovy-compile
            </classpathDependencyExclude>
        </classpathDependencyExcludes>
    </configuration>
</plugin>

4. Modules

The following sections display all bindings per module. Use this information to successfully override a binding on your own modules or to troubleshoot a module binding if the wrong type has been applied by the Griffon runtime.

4.1. Ebean

Module name: ebean

Depends on: datasource

bind(ResourceBundle.class)
    .withClassifier(named("ebean"))
    .toProvider(new ResourceBundleProvider("Ebean"))
    .asSingleton();

bind(Configuration.class)
    .withClassifier(named("ebean"))
    .to(DefaultEbeanConfiguration.class)
    .asSingleton();

bind(EbeanServerStorage.class)
    .to(DefaultEbeanServerStorage.class)
    .asSingleton();

bind(EbeanServerFactory.class)
    .to(DefaultEbeanServerFactory.class)
    .asSingleton();

bind(EbeanServerHandler.class)
    .to(DefaultEbeanServerHandler.class)
    .asSingleton();

bind(GriffonAddon.class)
    .to(EbeanAddon.class)
    .asSingleton();