'. '

LiveDB

From APIDesign

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
Another use of [[AnnotationProcessor]]s (in addition to generate [[CompileTimeCache]]s) is to provide type-safe, compile-time view of a live database structure. Something that might be called ''JavaOnRails''.
Another use of [[AnnotationProcessor]]s (in addition to generate [[CompileTimeCache]]s) is to provide type-safe, compile-time view of a live database structure. Something that might be called ''JavaOnRails''.
-
For a while languages like [[Ruby]] or [[Groovy]] has been blessed for being able to easily access structures in databases without all the hassle associated with traditional object relational mapping needed in [[Java]]. Maybe there was something more in the whole ''rails'' hype, but for me, ''rails'' mean exactly this. Be able to see content of [[LiveDB]].
+
For a while languages like [[Ruby]] or [[Groovy]] have been blessed for being able to easily access structures in databases without all the hassle associated with traditional object relational mapping needed in [[Java]]. Maybe there was something more in the whole ''rails'' hype, but for me, ''rails'' mean exactly this. Be able to see content of [[LiveDB]].
This page documents my experiment showing how to do something similar in good old [[Java]]. Well, it is not old good [[Java]], but [[Java]]6. [[Java]]6 has been around for few years already, but it is common that good ideas take time. As a result we are only slowly recognizing how useful [[AnnotationProcessor]]s can be.
This page documents my experiment showing how to do something similar in good old [[Java]]. Well, it is not old good [[Java]], but [[Java]]6. [[Java]]6 has been around for few years already, but it is common that good ideas take time. As a result we are only slowly recognizing how useful [[AnnotationProcessor]]s can be.

Revision as of 15:34, 29 July 2010

Another use of AnnotationProcessors (in addition to generate CompileTimeCaches) is to provide type-safe, compile-time view of a live database structure. Something that might be called JavaOnRails.

For a while languages like Ruby or Groovy have been blessed for being able to easily access structures in databases without all the hassle associated with traditional object relational mapping needed in Java. Maybe there was something more in the whole rails hype, but for me, rails mean exactly this. Be able to see content of LiveDB.

This page documents my experiment showing how to do something similar in good old Java. Well, it is not old good Java, but Java6. Java6 has been around for few years already, but it is common that good ideas take time. As a result we are only slowly recognizing how useful AnnotationProcessors can be.

Contents

Screen Cast

Thanks Geertjan for recording this screen cast with me!

Get the sources

The sources are now available and their execution is fully automated (even the Derby database is started automatically via the Ant build script). To get the sources follow these steps:

$ hg clone -r livedb http://source.apidesign.org/hg/apidesign/
$ cd apidesign
$ hg pull
$ hg update

The right project is located in samples/livedb directory. The project contains fully automated demo using Derby database (e.g. not MySQL as I used in the demo). Just use Ant to see how it behaves:

$ cd samples/livedb
$ ant test

Of course you can also open the project inside NetBeans IDE and experiement with it.

Create a database

Start Derby database as explained for example at JavaDB tutorial. Create a table called AGE with two columns NAME and AGE:

Image:Livedb.png

Use Code Completion

Now open the samples/livedb project in NetBeans IDE 6.9. Version 6.9 is the necessary minimum, as it is the first version that provides superior support for AnnotationProcessors.

Open for example LiveDBTest.java. It uses class Age. That class is dynamically generated during compile time to reflect structure of the AGE table created before. But try for example code completion on:

age.<ctrl+space>

You will correctly see fields representing individual rows in the t able. Moreover these fields have proper types (e.g. NAME is String, AGE is BigInteger). All of this is a result of simple use of the @LiveDB annotation in the package-info.java file:

@LiveDB(
    classname="Age", password="j1", user="j1",
    query="select * from APP.AGE", 
    url="jdbc:derby://localhost:1527/livedb"
)
package org.apidesign.livedb.example;
 
import org.apidesign.livedb.LiveDB;

Modify the Table

Now add new column to the table. Use it in the code. See if code completion sees it (probably not). If everything compiles (probably yes).

tbd.

Personal tools
buy