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.
            else if (pf.getJavaType() == UUID.class)
                newId = UUID.randomUUID();
            else if (pf.getJavaType() == (java.lang.Integer.class))
                newId = new Integer(persistentObject.getIncrementer().
            try {
                Method m = o.getClass().getMethod(ActiveMapperUtils.
    setterName(pf.getFieldName()), new Class[] { newId.getClass() });
                m.invoke(o, new Object[] { newId });
            } catch (NoSuchMethodException e1) {
            } catch (IllegalAccessException e1) {
            } catch (InvocationTargetException e1) {
            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>