Two posts ago, I showed how RIFE could be used to generate CREATE TABLE SQL statements.

Let me digress for just a paragraph about why I am exploring this technique to create database tables:

  • I'm hoping to use the Constraints mechanism used by RIFE to automate some DAO validation.
  • Using RIFE lets me use the same code to generate SQL specific to each database product.
  • My ultimate goal is to create a bean that actually creates the database table not just the SQL - more on this in future posts.
  • I dislike SQL scripts which don't provide the flexibility and robustness that Java provide - for example, logging and exception handling.
  • </ul> </p>

    Today, I demonstrate how to integrate Spring and RIFE. Here are the files that are involved:

    • - This interface specifies the methods used to generate SQL.
    • - This abstract class provides the CreateTable attribute to subclasses.
    • - This concrete class specifies the contstraints needed for the Beer database table.
    • - This driver file loads the Spring configuration file and instantiates beans.
    • spring.xml - This configuration file specifies the database configuration and wires the beans together.

    import com.uwyn.rife.database.queries.CreateTable;
    public interface DatabaseTableCreator {
        public String getSql();
        public String getTableName();
        public void setCreateTable(final CreateTable _createTable);
        public CreateTable getCreateTable();

    import com.uwyn.rife.database.queries.CreateTable;
    abstract public class BaseDatabaseTableCreator implements DatabaseTableCreator {
        private CreateTable createTable = null;
        public BaseDatabaseTableCreator() {
        abstract public String getSql();
        abstract public String getTableName();
        public CreateTable getCreateTable() {
            return this.createTable;
        public void setCreateTable(CreateTable _createTable) {
            this.createTable = _createTable;

    import org.activemapper.Beer;
    import com.uwyn.rife.database.DbConnection;
    import com.uwyn.rife.database.DbStatement;
    import com.uwyn.rife.database.queries.CreateTable;
    public class BeerTableCreator extends BaseDatabaseTableCreator {
        public BeerTableCreator() {
        public String getSql() {
            .precision("brand", 50)
            .nullable("brand", CreateTable.NOTNULL);
            return getCreateTable().getSql();li>
        public String getTableName() {
            return "beer";

    import org.springframework.context.ApplicationContext;
    public class PlayCreateTable {
         * @param args
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
            DatabaseTableCreator beerTableCreator = (DatabaseTableCreator)ctx.getBean("beerTableCreator");


    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' ''>
      <bean id='rifeDatasource' class='com.uwyn.rife.database.Datasource' lazy-init='true'>
        <property name='driver'><value>org.hsqldb.jdbcDriver</value></property>
        <property name='url' value='jdbc:hsqldb:file:data/relationalDb'/>
        <property name='user' value='sa'/>
        <property name='password' value=''/>
      <bean id='createTable' class='com.uwyn.rife.database.queries.CreateTable' lazy-init='true'>
        <constructor-arg ref='rifeDatasource'/>
      <bean id='beerTableCreator' class='' lazy-init='true'>
        <property name='createTable'><ref bean='createTable'/></property>

    I don't feel that I need to add any commentary, the code above speaks for itself. If you have questions, feel free to ask questions.