Sprinkl – Stuff for a Java Developer

A blog about things I don't want to forget

June 16, 2012
by Natan

HSQLDB pitfalls: column names and sequences

Pitfall one: column names

We recently started a Hibernate project with Postgresql as a database. And on of our database scripts looked like this

  "id" integer NOT NULL PRIMARY KEY,
  "name" VARCHAR(250)

And then we started doing our unit testing in HSQLDB a pretty neat in memory database. In memory database and unit testing is a perfect marriage IMHO. A simple migration of our database scripts was all that was needed.

Unfortunately it did not work. Executing a simple statement like

insert into person (id, name) values (10, 'Natan')

resulted in an obscure

Caused by: org.hsqldb.HsqlException:
  user lacks privilege or object not found: ID

It took us some time to figure out but it turns out it was not that obscure after all, the column in postgresql is named id but in hsqldb it is "id". Small difference! And I must say, after fixing the problem, HSQLDB behaves more logical.

Pitfall two: sequence numbers

For this we had a script in postgresql that created a sequence


which works fine. In fact it seemed to work fine in HSQLDB as well until we had a test that inserted two persons.

It turns out that sequences in HSQLDB are starting from 0 and that in combination with Hibernate it tries to insert both persons with ID 0.


simply fixed our problem.

In the end we got our unit tests running just fine but it took us a bit more time than expected.

June 5, 2012
by Natan

Using Apache Archiva with Gradle

This is an update of an interesting post I found on Using Archiva with Gradle. The post is a little outdated an I had to do a couple of small changes to get rid of the warnings and use the latest libraries.

apply plugin: 'maven'
group = 'com.example'
configurations {

dependencies {
  // needed for DAV protocol on maven uploads
  repoUpload "org.apache.maven.wagon:wagon-webdav-jackrabbit:2.2"

uploadArchives {
  uploadDescriptor = false
  repositories {
    ext.deployer = mavenDeployer {
      uniqueVersion = false
      configureAuth = {
        authentication(userName: 'uploader', password: 'haha')
      configuration = configurations.repoUpload
      snapshotRepository(url: "dav:http://example.com/archiva/repository/snapshots/", configureAuth)
      repository(url: "dav:http://example.com/archiva/repository/internal/", configureAuth)

Main changes are the upgrade of the ‘org.apache.maven.wagon:wagon-webdav-jackrabbit’ jar to version 2.2.

And to get rid of a build warning I changed ‘deployer’ to ‘ext.deployer’ to use the ExtraPropertiesExtension in gradle. And here I’m not even sure whether this was the right thing to do. Any tips would help!


May 29, 2012
by Natan

About CXF and problems with imports and JAXB

Last week we where trying to use a webservice that was actually build with Microsoft technology. And because one problem was not enough we had multiple just getting the webservice generated with tool wsdl2java tool.

Part of the WSDL looked like this.

          <xs:element minOccurs="0" maxOccurs="1" name="ListInfo">
                <xs:element ref="xs:schema" />
                <xs:any />

And the message I got was

WSDLToJava Error: Thrown by JAXB : undefined element 
declaration 'xs:schema'

It looks a bit weird but most tools do not seem to have problem with it. It took some searching on the internet and then I found this post.

Both solutions involve you modifying the WSDL unfortunately. So we went for the one where we replaced the original sequence with

          <xs:element minOccurs="0" maxOccurs="1" name="ListInfo">
                <xs:any maxOccurs="2" />

Note: the other solution is copy the wsdl locally and either add an
import for the schema namespace. Thanks Daniel Kulp.

May 11, 2012
by Natan

Getting started with gradle – part 2

Using a Maven repository

Setting up a repository is quite trivial

repositories {
    maven {
      url "http://repo1.maven.org/maven2"

Its my favorite repository hosted on http://www.mvnrepository.com.

Using Maven repositories

dependencies {
    compile 'ch.qos.logback:logback-classic:1.0.2'
    compile 'ch.qos.logback:logback-core:1.0.2'
    compile 'org.slf4j:slf4j-api:1.6.4'

    testCompile 'junit:junit:4.8.2'

Here we have 3 logging libraries which are setup for the project source code and a JUnit test library which will only be used during testing. If these libraries are not available locally the will be automatically downloaded on first usage.

May 9, 2012
by Natan

Making a Gradle build fail when JDepend has cycles

First we install the JDepend plugin.

apply plugin: 'jdepend'

Yes! It is that easy. It will download everything you need during your next gradle build.

Finally we come to the meat. I only want to detect cycles in actual source code and not the testing code so all I need to do is.

 jdependMain.doLast {
    File file = new File(jdepend.reportsDir, "main.xml");
    assert file.exists() && file.isFile(), "File '$file' must exist"

    def numberOfCycles = new XmlSlurper().parse(file).Cycles.Package.size()
    assert numberOfCycles == 0, """We have detected $numberOfCycles cycles. Checkout report file: $file.

This will lookup the report file after the JDepend has run on the ‘main’ folder. Then it will count the number of Package elements in the Cycles section. If it is not 0 it fails.

Let’s run

gradle build

And the build will stop with a nice error message.

C:\Git\demo>gradle build
:sms:compileJava UP-TO-DATE
:sms:processResources UP-TO-DATE
:sms:classes UP-TO-DATE

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Git\demo\build.gradle' line: 23

* What went wrong:
Execution failed for task ':sms:jdependMain'.
> java.lang.AssertionError: We have detected 2 cycles. Checkout report file: C:\Git\demo\sms\build\reports\jdepend\main.xml.
                                     . Expression: (numberOfCycles == 0). Values: numberOfCycles = 2

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.


Total time: 3.066 secs