Skip to content

synapticloop/h2zero

Repository files navigation

See http://synapticloop.github.io/h2zero/ for updated documentation

Build Status Download GitHub Release Gradle Plugin Release

This project requires JVM version of at least 1.7

Table of Contents top

h2zero top

lightweight ORM generator for mysql/sqlite, java with extensions for taglibs and routemaster

VERSION 3 top

This release is a pared down version of h2zero, with un-finished 
generators being removed and some placed into extensions

The following generators have been removed from h2zero

VERSION 2 top

This now only supports slf4j logging API and as such the 
"logging" key has been removed from the "options" JSON object

This is a object relationship mapper for MySQL and Java.

Whilst still a work in progress, database persistence is fully supported and running in production environments.

Extensions top

Extensions for h2zero are now supported see https://github.com/synapticloop/h2zero-extension-routemaster-restful

Background top

There are so many object relational mappers (ORMs) out there that do what h2zero does. It isn't special, it just provides a link between your database and generates your Java code to be able to use it.

Unlike other ORMs, you have full control of the SQL that is generated. So, if you

  • use MySQL
  • use Java
  • optionally use tomcat (JSPs/Tag Libraries/Servlets)

Then this is the ORM for you.

Unlike other ORMs, most (not all, but most) of the SQL must be hand-crafted by you, no horrible best-effort code generation, no horrific un-parseable SQL statements in the logs.

You have complete control over the SQL statements that are executed meaning that you can optimise your statements the way you want them, not the way that the generator thinks you should.

Your database, just the way that you designed it.

Requirements top

  • Java
  • MySQL
  • c3p0
  • Ant or gradle or command line usage

Creating a h2zero configuration file top

By default the h2zero file would look like the following:

{
	"options": {
		...
	},
	"database": {
		"schema": "...",
		"package": "...",
		"tables": [
			"fields": [
				{...}
			],
			"fieldFinders": [
				{...}
			],
			"finders": [
				{...}
			],
			"deleters": [
				{...}
			],
			"inserters": [
				{...}
			],
			"updaters": [
				{...}
			],
			"questions": [
				{...}
			],
			"counters": [
				{...}
			],
			"constants": [
				{...}
			],
		], 
		"views": [
			{...}
		]
	}
	
}

The CRUD operations (and some more) top

Create top

Normal plain old java objects (POJO). Just instantiate and insert (or insert silent)

Read top

Finders, Finders and more Finders

Update top

Updaters

Delete top

Deleters

and some more top

Counters - ever need just a simple count of the data that returns you the number of rows that match a specific sql statement?

This is what these are for:

"counters": [
  {
  "name": "countAllFlIsActive",
    "selectClause": "select count(*) from blog",
    "whereClause": "where fl_is_active = ?",
    "whereFields": [
      "fl_is_active"
    ]
  }
]

This will generate the following files

The Little Things top

  1. No more file changes for generation. If the h2zero file generation is the same, you won't see any file differences (unless you update to a newer version of h2zero that is) - we don't put dates, change-able comments or anything else in it
  2. We tell you what file was used to generate the code - we use the templar templating language, which is open source and easily modifiable.
  3. Easy editing of the templar files. If you don't like the way that we generate the file, you are free to modify it any way that suits you.
  4. Exceptions, darn those Exceptions. Whilst we based h2zero generation on the EJB model (and yes, people still use these things) - we felt that sometimes you just don't need to throw an exception if you already have an expectation of what you want. The classic example is the login page. 1. User logs in with email address and password 1. You try to find the user by email address and password 1. Couldn't find it - EXCEPTION, EXCEPTION, EXCEPTION 1. you can just choose to return null (by using the *Silent method signature) - in the above case this means that you couldn't find the user and happily display an error message, rather than littering your code with try/catch/finally code everywhere.

h2Zero generation top

gradle plugin top

Assuming that you have included the plugin

h2zero {
	inFile = 'src/test/resources/sample.h2zero'
	outDir = '.'
	verbose = 'false'
}

ant top

assuming that you have added the dependency above to the runtime configuration

task h2zero << {
	ant.taskdef(resource: 'h2zero.properties',
				classpath: configurations.runtime.asPath) {
	}

	ant.h2zero(inFile: 'src/main/resources/sample.h2zero',
				outDir: '.',
				verbose: 'false') {
	}
}

Command line generation top

try

  java -jar h2zero-all.jar

which will output:

Usage:

Command line usage:
===================

  java -jar h2zero-all.jar <mode> <options>

There are three (3) modes of operation, namely:

  generate  - this will generate the source code from the provided .h2zero file
  revenge   - this will reverse engineer a database to an .h2zero file
  quick     - this will generate a quick .h2zero file and output it to the console


generate options:
-----------------
  -in <arg>   the input file
  -out <arg>  the directory to output the generated files
  -verbose    turn on verbose output


revenge options:
----------------
  -host <arg>      the host of the database
  -database <arg>  the database
  -user <arg>      the user that can connect to the database
  -password <arg>  the password for the user
  -outFile <arg>   the file to write out the .h2zero file


quick options:
--------------
  -schema <arg>          the schema
  -generators <arg,...>  which generators to invoke
  -tables <arg,...>      which tables to generate
  -foreign <arg,...>     which foreign keys between the tables


Gradle build.gradle usage:
==========================

If you are using gradle, you can add this to your build.gradle file

  h2zero {
    inFile = 'src/main/resources/your_file_name_here.h2zero'
    outDir = '.'
    verbose = 'false'
  }


Ant build.xml usage:
====================

If you are using ant, you can add this to your build.xml file

  <path id="classpath-h2zero">
    <fileset dir="lib/runtime">
      <include name="*.jar"/>
    </fileset>
  </path>

  <target name="h2zero-generate" description="h2zero generate">
    <taskdef resource="h2zero.properties" classpathref="classpath-h2zero" />
    <h2zero inFile="src/main/java/your_file_name_here.h2zero" outDir="." verbose="false" />
  </target>

Exiting...

Building the Package top

*NIX/Mac OS X top

From the root of the project, simply run

./gradlew build

Windows top

./gradlew.bat build

This will compile and assemble the artefacts into the build/libs/ directory.

Note that this may also run tests (if applicable see the Testing notes)

Logging - slf4j top

slf4j is the logging framework used for this project. In order to set up a logging framework with this project, sample configurations are below:

Log4j top

You will need to include dependencies for this - note that the versions may need to be updated.

Maven

<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-slf4j-impl</artifactId>
	<version>2.5</version>
	<scope>runtime</scope>
</dependency>

<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-core</artifactId>
	<version>2.5</version>
	<scope>runtime</scope>
</dependency>

Gradle < 2.1

dependencies {
	...
	runtime(group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.5', ext: 'jar')
	runtime(group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5', ext: 'jar')
	...
}

Gradle >= 2.1

dependencies {
	...
	runtime 'org.apache.logging.log4j:log4j-slf4j-impl:2.5'
	runtime 'org.apache.logging.log4j:log4j-core:2.5'
	...
}

Setting up the logging:

A sample log4j2.xml is below:

<Configuration status="WARN">
	<Appenders>
		<Console name="Console" target="SYSTEM_OUT">
			<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
		</Console>
	</Appenders>
	<Loggers>
		<Root level="trace">
			<AppenderRef ref="Console"/>
		</Root>
	</Loggers>
</Configuration>

Artefact Publishing - Github top

This project publishes artefacts to GitHub

Note that the latest version can be found https://github.com/synapticloop/h2zero/releases

As such, this is not a repository, but a location to download files from.

All-In-One top

This project's artefact output is an 'all in one' jar which includes all runtime dependencies.

This should appear in the artefact repository along with the compiled code, as a convention, this is usually appended with an -all classifier

For example:

h2zero-3.1.6.jar -> h2zero-3.1.6-all.jar

Artefact Publishing - Bintray top

This project publishes artefacts to bintray

Note that the latest version can be found https://bintray.com/synapticloop/maven/h2zero/view

maven setup top

this comes from the jcenter bintray, to set up your repository:

<?xml version="1.0" encoding="UTF-8" ?>
<settings xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd' xmlns='http://maven.apache.org/SETTINGS/1.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <profiles>
    <profile>
      <repositories>
        <repository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>bintray</name>
          <url>http://jcenter.bintray.com</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>bintray-plugins</name>
          <url>http://jcenter.bintray.com</url>
        </pluginRepository>
      </pluginRepositories>
      <id>bintray</id>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>bintray</activeProfile>
  </activeProfiles>
</settings>

gradle setup top

Repository

repositories {
	maven {
		url  "http://jcenter.bintray.com" 
	}
}

or just

repositories {
	jcenter()
}

Artefact Publishing - gradle plugin portal top

This project publishes artefacts to the gradle plugin portal

Note that the latest version can be found https://plugins.gradle.org/plugin/synapticloop.h2zero

Dependencies - Gradle top

dependencies {
	runtime(group: 'synapticloop', name: 'h2zero', version: '3.1.6', ext: 'jar')

	compile(group: 'synapticloop', name: 'h2zero', version: '3.1.6', ext: 'jar')
}

or, more simply for versions of gradle greater than 2.1

dependencies {
	runtime 'synapticloop:h2zero:3.1.6'

	compile 'synapticloop:h2zero:3.1.6'
}

Dependencies - Maven top

<dependency>
	<groupId>synapticloop</groupId>
	<artifactId>h2zero</artifactId>
	<version>3.1.6</version>
	<type>jar</type>
</dependency>

Dependencies - Downloads top

You will also need to download the following dependencies:

cobertura dependencies

  • net.sourceforge.cobertura:cobertura:2.1.1: (It may be available on one of: bintray mvn central)

compile dependencies

runtime dependencies

shadowRuntime dependencies

testCompile dependencies

testRuntime dependencies

NOTE: You may need to download any dependencies of the above dependencies in turn (i.e. the transitive dependencies)

License top

The MIT License (MIT)

Copyright (c) 2018 synapticloop

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

--

This README.md file was hand-crafted with care utilising synapticlooptemplar->documentr

--