A Java CRUD generator (and PHP, and Ruby, and ...)

Way back in the late 1990s, I wrote a Java CRUD generator, which was based on the work of someone else. It was a static code generator, but like future dynamic frameworks like Ruby on Rails, CakePHP, and others, it scanned the database and generated source code from the database table definitions.

I showed it to a co-worker, and he suggested the idea of making it a dynamic framework (like Rails would soon be), but as timing would have it, I started doing a lot of work against legacy As/400 databases, whose 'tables' and 'fields' had relatively bizarre names, and I dropped the idea.

2010 - Convention over configuration

Fast-forward to 2010, and as I've been working with various frameworks again, it seems like many frameworks have adopted the "convention over configuration" approach, and they have their own ways of turning database tables into MVC (model-view-controller) code, be it a static or dynamic approach.

Most of this strikes me as good, but some of it is bad. For instance, at least one tool which takes this approach requires Maven, and I personally don't like Maven, and don't want to be forced to use it. I also don't like having to learn a different ORM syntax for every framework.

As I also need to work with different programming languages to support my clients, I finally rolled all of this into one thought:

What if you had a programming language-independent way of generating CRUD? That is, whether your programming language of choice is Java, PHP, Ruby, Python, whatever, what if there was one tool that could generate your CRUD files for any of these languages?

If you had a CRUD generator tool like this, could your development time be almost as fast as Rails or CakePHP development time, without getting pulled into the entire Rails or CakePHP framework? (That is, you can use any framework you want.)

 

For all I know this type of tool already exists, but to exercise my own PHP skills, I've been writing my own programming-language independent CRUD generator tool over the last few days, and it should be in very good shape in later this week.

The language-independent CRUD generator concept

The basic concepts behind my CRUD generator are simple:

  • Generate CRUD for any programming language: Java, PHP, Ruby, Python, etc.
  • Let you generate anything you want: Models, views, controllers, configuration files (for Hibernate, Spring, etc.), whatever.
  • Provide support to generate "anything" from the database table definitions by the use of templates. Just thinking about Java for a moment, with a templating approach you can create all sorts of Java CRUD:
    • Java model classes
    • Java controller classes (Spring, Struts, WebWork, etc.)
    • Java Dao classes (Spring Dao classes, Hibernate configuration files, whatever)
    • Java view files (JSP files, JSF files)
    • Java configuration files (Spring applicationContext.xml, JSF files, Hibernate, whatever)

Because my CRUD generator tool only deals with templates, it doesn't care what you generate -- that's up to you. You provide the templates, I scan the database and turn your template files into whatever they're supposed to be.

This simple concept of templates -- plus letting you configure all sorts of parameters -- makes this tool language-independent, and framework-independent. As long as there's a template for what you want to generate, you can generate it.

Application Usage

Okay, I just beat on the app some more today, and here's the current usage statement that shows how to invoke my [Java|PHP|Ruby|C#|JSF|Hibernate|etc] CRUD generator:

=== USAGE ===

If your database follows the normals Rails-like naming 
convention you can invoke this script like this:

  db gen model User

If you don't (or can't) follow that convention and you
want to create an object named User to map to a
database table named peeps, you can invoke this script 
like this:

  db gen model User peeps

Now that you've seen those two specific instances, the
generic invocations look like this:

  db gen model [ClassName]
  db gen model [ClassName] [table_name]

  db gen controller [ClassName]
  db gen controller [ClassName] [table_name]

Actually, that's a bit of a fib to make this app look 
more like Rails, and make it all easier to absorb ...
the real invocations look like this:

  db gen [template-name] [ClassName]
  db gen [template-name] [ClassName] [table_name]

The reason the earlier invocations work is that it's 
standard to have templates named model.tpl, view.tpl, 
and controller.tpl, but in fact you can have templates
with all sorts of names:

  model.tpl
  view.tpl
  controller.tpl
  applicationContext.tpl
  hibernate.tpl
  faces-beans.tpl
  faces-config.tpl
  navigation-rules.tpl
  whatever-you-want.tpl

Once you have a template named foo.tpl set up, you can
run this script like this:

  db gen foo [ClassName]

or

  db gen foo [ClassName] [table_name]

 

More CRUD generator information coming soon

I'll write more about my CRUD generator application soon, but I thought I'd just kick it off here today.

Update: This software is now publicly available. See my Programming Language-Independent CRUD Generator page for more information.