Saturday, May 17, 2008

Maven2 hibernate DDL generation

I needed a way to get hibernate to generate DDL for me and I ended up using the hibernate maven plugin. It took a lot of trial and error to get it working so hopefully this will save someone some time when they try to do the same thing.

In case you are not familiar, Hibernate is an ORM package for Java that allows you to treat your database like a collection of objects (sort of). Maven2 is a java build system (like ant or make). DDL is data definition language and tends to be unique for every database so if you want a project to work with multiple databases then you have to create DDL for each one. That's where this comes in to play. Normally hibernate will generate DDL and run it for you but this is not always desirable. Especially when dealing with production systems which already have a large database. This will allow you to generate the DDL right in the source of your project and then simply commit the DDL files back to your source repository. If you change your hibernate mapping files then regenerate the DDL and commit the changes.

I created a simple folder with the right pom.xml and the other needed support files to allow a developer to generate DDL by just running mvn install in their ddl folder (the one with these files). You can download that to get started. There is a little bit of setup required which is detailed on my wiki page about generating DDL using hibernate and maven2. Checkout the readme which explains how to use it. I would think it is probably 10 minutes of effort to add this to an existing hibernate project which is using maven 2.

Sunday, May 11, 2008

Fun with Maven 2 dependencies

I learned some new Maven 2 tricks this weekend while experimenting with things. In general it can be a real pain when you are developing a piece of framework code which a few projects depend on and you find a minor issue which requires a new release. Typically you have to go around updating lots of POMs. Well, no longer! In maven 2 you can now specify version ranges which can save you a lot of updating when newer versions of packages come out.
Normal maven 2 depedencies look like this:


<dependency>
<groupId>org.sakaiproject.entitybroker</groupId>
<artifactId>entitybroker-api</artifactId>
<version>1.3.3</version>
<scope>provided</scope>
</dependency>


Unfortunately, if a bug fix is released for this which is 1.3.4 then I will not pick up on the new version and would have to explicitly put it in. However, if I want to pick up any minor version updates for a project and I want the minimum version to be 1.3.3 (but less than 1.4.0) I can do this:


<dependency>
<groupId>org.sakaiproject.entitybroker</groupId>
<artifactId>entitybroker-api</artifactId>
<version>[1.3.3,1.4.0)</version>
<scope>provided</scope>
</dependency>

A word of warning though, if the packages you are using do not adhere to standards of versioning you can really get in trouble if a new version comes out with an incompatible interface. Of course, if that happens you simply adjust the version range in the POM.

More info here: http://docs.codehaus.org/display/MAVEN/Dependency+Mediation+and+Conflict+Resolution

Thursday, May 08, 2008

EntityBroker 1.3.3 released

The new 1.3.3 version of EntityBroker is released. This includes a bunch of new features to make working with Sakai easier but the biggest improvement is full support for RESTful URLs. Now a developer can write an EntityProvider and implement a handful of interfaces and end up with REST access to their entities. This now allows developers to expose data from within Sakai to be read and written via REST calls by other Sakai apps, Sakai helpers, external apps (PHP, Perl, etc.), and client side widgets. This includes support for transformation of entity POJOs to and from JSON and XML automatically. Like everything else in EB, this can be disabled or controlled explicitly if desired. More details on the RESTful entities page.
The other major change in this release is the DeveloperHelperService. This is a shared service that the EntityBroker provides. This service makes it easier to deal with current Sakai data as entity references. Methods include getting current user/location/tool and generating URLs to work with entites and within Sakai. It also provides for some protection against shifting Sakai service APIs as this service API will remain stable and allow access to common things like current user and site without requiring knowledge of the Sakai service structure.
There are few minor improvements in the new version like support for persistent entity properties (without using hibernate) and support for persistent entity tags. There are also improvements in the classloader handling to ensure that the entity system will not hold onto class resources when a classloader reloads (e.g. webapp reload). Thanks to testing by Stephen Marquard this release should be fully backwards compatible with the pervious version.

Sunday, May 04, 2008

GenericDao 0.9.5

The 0.9.5 version of GenericDao is now available. This provides the biggest leap in functionality yet with enhancements including support for JDBC and pure SQL (along with Hibernate) and new search methods which use a generalized Search object to make it easy to control restrictions, limits, ordering and bascially everything you would want to put in a search for an object.
For anyone unfamiliar with the package it is mostly a bunch of convenience code to make it a little easier to have ORM like functionality and not have to write the same boring create/update/find/delete methods over and over again. It also provides a simple and extendable interface for working with persistent objects.
I am using the new version to replace the previous hibernate usage in a couple of projects and things seem to work quite well so far. The new version builds on some of the Apache projects like BeanUtils, Google projects like Guice, and the Spring Framework.
I hope to add in support for DDL generation using Apache DDLUtils in the next release.

Download (Maven2):https://source.sakaiproject.org/maven2/org/sakaiproject/generic-dao/
Maven site: https://source.sakaiproject.org/maven2/org/sakaiproject/generic-dao/site/
Javadocs: https://source.sakaiproject.org/maven2/org/sakaiproject/generic-dao/site/apidocs/

On a related note, this release was made using the Maven 2 release plugin which made the process very clean and easy. I recommend it highly.