Working with Custom Property Types

In addition to configuring Extended Types as described in Cayenne Guide, users must pay attention to serialization of the custom property types. A custom class must be serializable so that it can be passed between the client and the server. Cayenne uses a library called Hessian to perform the serialization of objects. Mostly it will just work with custom extended types, but there are some important requirements:

  • when Hessian deseralizes the stream back into an object it will construct that object by looking for a bean constructor with no parameters. If that isn't found, then the next shortest constructor is called with all parameters set to null. Your extended type class needs to support this. If your extended type subclasses BigDecimal for example, by default this will result in an exception (null is not allowed), so you'll need to add another constructor.
  • Hessian will then set the values for all fields in your custom extended type, except those set as transient. These fields therefore need to be serializable in themselves.
  • You can optionally define some additional helper functions within your extended type which Hessian will use.
    private Object writeReplace() throws ObjectStreamException {
    	// return some object which Hessian will serialize instead of your extended type
    private Object readResolve() throws ObjectStreamException {
    	// return the final object in its deserialised form

    Note that the functions are private - that is fine and encouraged since these functions should not normally be available to a subclass.

Instead of all the above you may be able to register an additional SerializerFactory to handle your extended type by using factory). This will give you complete control over the process.