Tuesday, April 12, 2016

How to Generate Customized Java 8 Code with Plugins

One thing most programmers hate is to write boilerplate code. Endless hours are spent setting up entity classes and configuring database connections. To avoid this you can let a program like Speedment Open Source generate all this code for you. This makes it easy to get a database project up and running with minimal manual labour, but how do you maintain control of the written code when large parts of it is handed over to a machine?

Say that you have a database with a table "user" which has a column "gender", and you want that implemented as an enum in java. If you run Speedment and use it to generate code, the "gender" field will be represented as a String. The reason for this is that there isn’t any built-in mappers for converting between database ENUMs and custom java classes. This is one of those cases when you might feel that the generator is taking away control for you. Well, fear not, for since the 2.3 Hamilton release, you can get this same control by creating your own plugin for Speedment!

The Goal of this Article

In this example we have a database schema with a table called "Person". A person has an id, a name and a gender. The gender is declared as an ENUM with three possible values: "Male", "Female" and "Other". If we use the default settings in Speedment to generate this class, Speedment will consider the ENUM a String. There are some issues with this however. For an example, if you want to persist a new person into the database, there is nothing that prevents you from spelling a gender wrong and getting an exception when you do the insert. Instead, we want to define a java enum with the specified alternatives as constants. What would make the generated code more secure and easier to use.

We can achieve this using a plugin for Speedment!

Creating the Plugin Project

To do any custom modifications to the Speedment platform we will need to define a plugin. A plugin is a piece of software that can be plugged into the Speedment runtime from the pom.xml-file. The plugin resides in its own maven project and can be shared between projects.

Begin by creating a new Maven Project and declare Speedment as a dependency. You will not need the speedment-maven-plugin in this project.


The plugin system revolves around two interfaces; Component and ComponentConstructor. A Component is a pluggable piece of software that can be executed as part of the Speedment lifecycle. Every component has a number of stages in which it is allowed to execute. These are "initialize", "load", "resolve" and "start".

The ComponentConstructor is a lightweight type that has a default constructor and a method for initializing new instances of the custom component. This is used by the maven plugin to setup the new code.

Here is how our two implementations will look:


public final class CustomMappingComponent 
extends AbstractComponent {
    CustomMappingComponent(Speedment speedment) {

    public void onResolve() {
        // Resolve logic here...

    public Class<CustomMappingComponent> getComponentClass() {
        return CustomMappingComponent.class;

    public Software asSoftware() {
        return AbstractSoftware.with(
            "Custom Mapping Component", 

    public Component defaultCopy(Speedment speedment) {
        return new CustomMappingComponent(speedment);

public final class CustomMappingComponentInstaller 
implements ComponentConstructor<CustomMappingComponent> {

    public Component create(Speedment speedment) {
        return new CustomMappingComponent(speedment);

We now have a bare-bone plugin that can be added to a Speedment project. The next step is to define the logic that maps between strings and genders. To this this, first we need the Gender enum.


public enum Gender {
    MALE   ("Male"), 
    FEMALE ("Female"),
    OTHER  ("Other");

    private final String databaseName;

    Gender(String databaseName) {
        this.databaseName = databaseName;

    public String getDatabaseName() {
        return databaseName;

If you store the enum values in upper-case in the database, this class could be much shorter since you could simply use the Enum.name()-method to get the database name, but this approach is better if you want flexibility in naming the constants.

Now for the final piece. We need to declare a type that implements the TypeMapper-interface in Speedment. A type mapper is really simple. It contains two methods for mapping to and from the database type as well as methods for retrieving the java class of both types.


public final class StringToGenderMapper implements TypeMapper<String, Gender> {
    public Class<Gender> getJavaType() {
        return Gender.class;

    public Class<String> getDatabaseType() {
        return String.class;

    public Gender toJavaType(String value) {
        if (value == null) {
            return null;
        } else {
            return Stream.of(Gender.values())
                .filter(g -> g.getDatabaseName().equals(value))
                .orElseThrow(() -> 
                    new UnsupportedOperationException(
                        "Unknown gender '" + value + "'."

    public String toDatabaseType(Gender value) {
        if (value == null) return null;
        else return value.getDatabaseName();

    public boolean isIdentityMapper() {
        return false;

This new mapper also need to be installed in the Speedment platform. We can do that from the component we created earlier by modifying the onResolve()-method:


public void onResolve() {
    // Resolve logic here...

Our new plugin is now done! Build the project and you are set to go!

Using the Plugin

To use a plugin in a project, you only need to modify the pom.xml-file of that project. Open up an existing Speedment project and locate the pom.xml-file. In it, you should be able to find the speedment-maven-plugin. To make your own plugin accessible for the maven plugin, you need to add it as a dependency inside the <plugin>-tag and add the ComponentInstaller to the configuration. Here is an example of how it can look:




        <!-- Our plugin project -->
            <!-- Path to the component installer -->
            <component implementation="
            " />

You also need to add the project as a runtime dependency since the new Gender-enum must be accessible from the generated code.


Trying it out

That’s it! The plugin is installed! If you want to a particular column to be mapped to a Gender instead of a String, you can go into the User Interface, navigate to the particular column in the "Project Tree" and select your new Type Mapper in the dropdown list.

screenshot of the Speedment user interface

If you want to see a list of all the components and/or type mappers loaded into the platform, you can also go to "About" → "Components..." in the UI. There you should see the new component.

screenshot of the components dialog in the Speedment user interface


In this article you have learned how to create a custom plugin for Speedment that integrates a new Type Mapper from a String to a Gender enum. You have also learned how you can see which components are loaded into the platform and select which type mapper you want to use for each column.

PS: If you create some cool new mappers for your Speedment project, consider sharing them with the community in our Gitter chat!

Friday, April 8, 2016

Java 8: Bye Manual SQL, Hello Speedment!

Spire says Goodbye to the old way of writing
database applications in Java.

Most applications written in Java require some form of data storage. In small applications this is often realized using a primitive JDBC-connection that is queried using ordinary SQL. Larger systems on the other hand often use an Object Relational Mapping (ORM) frameworks to handle the database communication. There are pro’s and con’s with both of these approaches, but both tend to involve writing a lot of boilerplate code that looks more or less the same across every codebase. In this article I will showcase another approach to easy database communication using an open source project called Speedment.

What is Speedment?

Speedment is a developer tool that generates java classes from your SQL metadata. The generated code handles everything from setting up a connection to data retrieval and persistence. The system is designed to integrate perfectly with the Java 8 Stream API so that you can query your database using lambdas without a single line of SQL. The created streams are optimized in the background to reduce the network load.

Setting Up a Project

In this article I will write a small application that asks for the user’s name and age and persist it in a MySQL database. First of, we will define the database schema. Open up your MySQL console and enter the following:

CREATE DATABASE hellospeedment;
USE hellospeedment;

    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `name` varchar(32) NOT NULL,
    `age` int(5) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `name` (`name`)

Next we will create our java project. Fire up your favorite IDE and create a new Maven Project from Archetype. Archetypes are template projects that can be used to quickly define new maven projects. Exactly how they are used differ between different IDEs. The following information will have to be entered:

Repository https://repo1.maven.org/maven2
GroupId com.speedment
ArtifactId speedment-archetype-mysql
Version 2.3.0

Similar archetypes are available for PostgreSQL and MariaDB as well.

On NetBeans, the archetype is usually found among the default ones indexed from the Maven Central Repository. When the project is created you should have something like this:

Launching the Speedment UI

Now then the project has been created it is time to start up the Speedment User Interface. This is done by executing the speedment:gui-maven goal. In NetBeans and IntelliJ IDEA, a list of available maven goals can be found from within the IDE. In Netbeans this is found in the Navigator window (often located in the bottom-left of the screen). The project root node must be selected for the goals to appear. In IntelliJ, the goals can be found under the "Maven Projects"-tab in the far right of the screen. You might need to maximize the "Project Name", "Plugins" and "speedment-maven-plugin"-nodes to find it. In Eclipse, you don’t have a list of goals as far as I know. Instead you will have to define the goal manually. There is a tutorial for doing this on the Speedment GitHub wiki.

When the user interface starts the first time it will ask for your email address. After that you can connect to your database.

The connection dialog will only allow you to choose between databases that you can connect to using the loaded JDBC-drivers. If you for example want to use a PostgreSQL-database, you should add the PostgreSQL-driver to the <dependencies>-tag of the speedment-maven-plugin section in the pom.xml-file and the re-run the UI.

Once you have connected to the database, the main window opens. On the left side you can see a tree-view of the database. In the middle is the workspace where things like database connection, code generation and entity naming can be configured. You can select what part of the project to configure by selecting other nodes in the tree.

In this case, we will simply press the "Generate"-button in the toolbar to generate a project using the default settings. We can now close the UI and return to our IDE.

Write the Application

Now when Speedment has generated all the boilerplate code required to communicate with the HelloSpeedment database we can focus on writing the actual application. Let’s open the Main.java-file created by the maven archetype and modify the main() method.

public class Main {
    public static void main(String... params) {
        Speedment speedment = new HellospeedmentApplication()
        Manager<User> users = speedment.managerOf(User.class);

In Speedment, an application is defined using a builder pattern. Runtime configuration can be done using different withXXX()-methods and the platform is finialized when the build()-method is called. In this case, we use this to set the MySQL password. Speedment will never store sensitive information like your database passwords in the configuration files so you will either have to have a unprotected database or set the password at runtime.

The next thing we want to do is to listen for user input. When a user starts the program, we should greet them and then ask for their name and age. We should then persist the user information in the database.

final Scanner scn = new Scanner(System.in);

System.out.print("What is your name? ");
final String name = scn.nextLine();

System.out.print("What is your age? ");
final int age = scn.nextInt();

try {
} catch (SpeedmentException ex) {
    System.out.println("That name was already taken.");

If the persistence failed, a SpeedmentException is thrown. This could for example happen if a user with that name already exists since the name column in the schema is set to UNIQUE.

Reading the Persisted Data

Remember I started out by telling you how Speedment fits in nicely with the Stream API in Java 8? Let’s try it out! If we run the application above a few times we can populate the database with some users. We can then query the database using the same users manager.

        .collect(joining(",\n    ", "[\n    ", "\n]"))
This will produce a result something like this:



This article has showcased how easy it is to write database applications using Speedment. We have created a project using a maven archetype, launched the Speedment UI as a maven goal, established a connection with a local database and generated application code. We have then managed to do both data persistence and querying without a single row of SQL!

That was all for this time.

PS: Speedment 2.3 Hamilton was just released the other day and it contains a ton of really cool features for how you can manipulate the code generator to fit your every need. Check it out!