Dashboard > RIFE > ... > Tips and Tricks > Generating a DAO layer from POJOs
RIFE Log In | Sign Up   View a printable version of the current page.
Generating a DAO layer from POJOs


Added by Christian Vest Hansen, last edited by Christian Vest Hansen on May 09, 2007  (view change) show comment
Labels: 
(None)

With POJO peristence and MetaData classes, RIFE can generate your database tables in a database independant manner. Each of your POJO bean classes will represent a single table in the database, and each instance of these classes represents a signle row in their respective table. The MetaData classes are used to describe the object-relational mapping between the data bean, and the table. This mapping is described with constraints; for instance, this field in the bean class:

private int id;

Would most likely be mapped using this constraint in the corrosponding MetaData class:

addConstraint(new ConstrainedProperty("id").identifier(true).unique(true));

This way, RIFE applications that uses databases as persistence, will often at least two classes per database table. And for each column in all of these tables, a field, with associate getters and setters, and a constraint declaration will exist.

Although RIFE is very lightweight in this regard, compared Hibernate, and especially EJBs, it can still be terse to write all these classes. Most IDEs show you a little wizard you need to step through every time you want to create a new class, so even though your IDE can generate the getters and setters for the fields in your data bean, you still have to do a lot of trivial (boring) clicking and typing before your MetaData classes are up to speed with you data beans.

So the question that now arises is; how can we reduce the trivial parts of this task, and let the programmer focus on the non-trivial parts? How can we speed this up?

A possible way is to have a little script generate stub code for our MetaData classes, based on the fields in our data beans.

Attached to this page is a Python script that does just this, called RifeDaoGen.py.

You use it by putting the script file in the same directory as your databeans, and then just execute it with no arguments.

It'll expect every java file in the working directory, to be a data bean class; with the exception of those ending in 'MetaData.java', who will instead be skipped. The script will then create a *MetaData.java file for every other .java file in the directory, and it will also try to add constraint declarations for all the private fields in these java files.

Perhaps an example shows best what is actually generated. If you have this file in your /com/app/model directory:

User.java
package com.app.model;

public class User {
	private long id;
	private String username;
	private String passwordHash;
	private String fullname;
	private String email;
	private long joinDate;
	private boolean isEmailPublic = false; // e-mail isn't public by default
	
	// lots of getters & setters
}

Then the following file will be generated for you:

UserMetaData.java
/*
 * Generated with RifeDaoGen.py - v1.2
 */

package com.app.model;

import com.uwyn.rife.site.ConstrainedProperty;
import com.uwyn.rife.site.MetaData;

@SuppressWarnings("unchecked")
public class UserMetaData extends MetaData {
	public void activateMetaData() {
		addConstraint(new ConstrainedProperty("id").identifier(true).unique(true));
		addConstraint(new ConstrainedProperty("username").notNull(true));
		addConstraint(new ConstrainedProperty("passwordHash").notNull(true));
		addConstraint(new ConstrainedProperty("fullname").notNull(true));
		addConstraint(new ConstrainedProperty("email").notNull(true).email(true));
		addConstraint(new ConstrainedProperty("joinDate").notNull(true));
		addConstraint(new ConstrainedProperty("isEmailPublic").notNull(true).defaultValue(false));
		// TODO MetaData stub
	}
}

Note:
The latest version of this script will also generate Manager classes and an InstallParticipant.



Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.1a Build:#515 May 19, 2006) - Bug/feature request - Contact Administrators