Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 2: Ninja

Matthew Tyson | Nov. 25, 2015
Build a functional Person application with Ninja.

Now consider the infrastructure required to make this entity work. We'll need three things:

  1. A database for storage
  2. A JPA configuration
  3. A bit of Ninja config

Ninja supports anything that JPA can, using Hibernate as the provider, so you have your choice of virtually any major SQL database. For the example app I'll use MariaDB, a MySQL fork developed by some of MySQL's original designers. MariaDB installs and operates much like MySQL.

Once you've selected and installed a database, add a schema called ninja_app using the create command like so: create database ninja_app. Don't forget to also add your chosen database connector to your pom.xml! In this case I added the mysql-connector-java dependency.

Next, we'll include a JPA configuration file, so our app knows how to connect to the database.

JPA configuration

Listing 2 displays the contents of our persistence.xml file, and reveals itself to be a typical JPA config.

Listing 2. /src/main/java/META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns=""




    <!-- Database settings for development and for tests -->

    <persistence-unit name="devdb" transaction-type="RESOURCE_LOCAL">



            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

            <property name="hibernate.show_sql" value="true" />

            <property name="hibernate.format_sql" value="true" /> 

            <!-- Connection Pooling settings -->

            <property name="hibernate.connection.provider_class"

                value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />

            <property name="hibernate.c3p0.max_size" value="100" />

            <property name="hibernate.c3p0.min_size" value="0" />

            <property name="hibernate.c3p0.acquire_increment" value="1" />

            <property name="hibernate.c3p0.idle_test_period" value="300" />

            <property name="hibernate.c3p0.max_statements" value="0" />

            <property name="hibernate.c3p0.timeout" value="100" />      




The code in Listing 2 is a mostly generic configuration, targeting a MySQL instance. The key part for us is the configuration profile (aka persistence-unit) named devdb. Also note that the transaction-type is RESOURCE_LOCAL, meaning we're using local transactions.


Previous Page  1  2  3  4  5  6  7  8  Next Page 

Sign up for CIO Asia eNewsletters.