How to map typesafe enumerations with Castor

# How to map typesafe enumerations with Castor

## Intended Audience

Anyone who wants to persist object that refer to a typesafe enumeration.

This document addresses the basics and shows an example how to map an object that has a typesafe enumeration property.

## Prerequisites

Enumerations are a common method for ensuring data integrity, both in software and in relational databases. As a platform for linking the two, we added support for persisting class fields whose type is a Java typesafe enumeration to Castor JDO.

To use this new feature your typesafe enumeration should follow the enum pattern commonly used and provide a static valueOf(String) method. An enum of different kinds of computer equipment may look like:

    public class KindEnum {
        private static final Map KINDS = new HashMap();

        public static final KindEnum MOUSE = new KindEnum("Mouse");
        public static final KindEnum KEYBOARD = new KindEnum("Keyboard");
        public static final KindEnum COMPUTER = new KindEnum("Computer");
        public static final KindEnum PRINTER = new KindEnum("Printer");
        public static final KindEnum MONITOR = new KindEnum("Monitor");

        private final String _kind;

        private KindEnum(final String kind) {
            _kind = kind;
            KINDS.put(kind, this);
        }

        public static KindEnum valueOf(final String kind) {
    	    return (KindEnum) KINDS.get(kind);
        }

        public String toString() { return _kind; }
    }

At your Product class you may want to have a property that tells you what kind of computer equipment a product is of.

    public class Product {
        private int         _id;
        private String      _name;
        private KindEnum    _kind;

        public Product() { }

        public int getId() { return _id; }
        public void setId(int id) { _id = id; }

        public String getName() { return _name; }
        public void setName(String name) { _name = name; }

        public KindEnum getKind() { return _kind; }
        public void setKind(KindEnum kind) { _kind = kind; }
    }

## Steps

Your mapping for the Product class should be:

    <class name="Product" identity="id">
      <description>Product with kind enum</description>
      <map-to table="enum_prod"/>
      <field name="id" type="integer">
        <sql name="id" type="integer"/>
      </field>
      <field name="name" type="string">
        <sql name="name" type="char"/>
      </field>
      <field name="kind" type="KindEnum">
        <sql name="kind" type="char"/>
      </field>
    </class>

## Tips

- To add this new feature we added an additional check when searching for field types. Like before Castor first searches for know types and thereafter for a mapping for the class you specified as type. If both of them do not match it now checks if the class specified as type is available at classpath and has a static valueOf(String) method. Only if all of this conditions are met it will be viewed as a valid mapping.