2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2020

11/01/2005: Using RIFE CreateTable to Generate CREATE TABLE Sql

The RIFE framework has functionality which abstracts the Sql Create Table statement into Java classes. The fundamental reason to use an abstraction instead of writing the Sql directly is that the underlying framework generates Sql specific to the targeted database. Why is this important? Frequently I use an open-source database like Hypersonic for development but a commercial product like Oracle for production.

The following code demonstrates the technique:

  String driverClassname = "org.hsqldb.jdbcDriver";
  String url = "jdbc:hsqldb:hsql://localhost:9101/test";
  String username = "sa";
  String password = "";
  String poolSize = 5;

  Datasource ds = new Datasource(driverClassname, url, username, password, poolSize);

  CreateTable create = new CreateTable(ds);

  create.table("beer")
    .columns(Beer.class)
    .primaryKey("id")
    .precision("brand", 50)
    .nullable("brand", CreateTable.NOTNULL);

  String createSql = create.getSql();

When executed, the generated SQL looks like this:

CREATE TABLE beer (brand VARCHAR(50) NOT NULL, id INTEGER NOT NULL, price NUMERIC, PRIMARY KEY (id))

For completeness, here are the relevant parts of Beer.java

public class Beer {

    private String brand = null;

    private BigDecimal price = null;

    private int id = 0;

    // ... snipped out getters and setters.
}

11/01/2005: ActiveMapper - Using UUID Values for Primary Keys

I am a big fan of using UUID values as primary keys but I won't use this blog to articulate why. Other people in the blogosphere have argued back and forth on the subject of primary keys - you don't need me to repeat the arguments.

If you do want to use UUID values, then ActiveMapper will not be useful in its current version. The good news is that adding support is trivally easy.

In order to use UUID values with ActiveMapper you need to do three things:

  1. Update the ActiveMapper.assignNewId() method as shown below - just add the two highlighted lines.
        protected Object assignNewId(Object o) {
            PersistentField pf = (PersistentField) persistentObject.getPersistentFields().get("id");
            Object newId = null;
            if (pf.getJavaType() == (java.lang.Long.class))
                newId = new Long(persistentObject.
    getIncrementer().nextLongValue());
            else if (pf.getJavaType() == UUID.class)
                newId = UUID.randomUUID();
            else if (pf.getJavaType() == (java.lang.Integer.class))
                newId = new Integer(persistentObject.getIncrementer().
    nextIntValue());
            try {
                Method m = o.getClass().getMethod(ActiveMapperUtils.
    setterName(pf.getFieldName()), new Class[] { newId.getClass() });
                m.invoke(o, new Object[] { newId });
            } catch (NoSuchMethodException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            } catch (InvocationTargetException e1) {
                e1.printStackTrace();
            }
            return newId;
        }
    
  2. Use the following instance variable in your domain objects:
    private UUID id = null;
    
  3. Use the following field definition when creating SQL tables:
    id char(36) not null
    
    </il> </ol>