This article is more involved with integrating java objects with existing frameworks than the structure/usage of an immutable class.
To use an immutable object with hibernate; the access-type must be configured to use fields. The default access type can vary upon usage. When a class file is mapped via XML, the access type is set to properties (values are retrieved and saved via the getters and setters). Under this condition set the default-access property [under the “default-mapping” tag] to “field.” If hibernate is configured to use annotations, make sure the @id annotation is positioned above one of the fields. With hibernate configured to used fields, the values will be inserted directly into the actual values. Note: Since the class is immutable, an update will never be needed for the class. The only possible operations that can preform with an immutable object are saving [insert], querying, and deleting.
JAXB is a recently included XML serialization library, it is often found in libraries dealing with web services. Its purpose is to convert an object into an XML based representation. It operates in a similar fashion as Hibernate, and requires a similar configuration. To force JAXB to use the instance members, rather than the setters, use the annotation “XmlAccessorType” above the class, and set the value to XmlAccessType.Field. Additionally, it requires a default [zero argument constructor], however that may (and should) be marked private.
Making immutable classes serializable is not as easy as it is with mutable classes. With mutable classes, implementing the Serializable interface [which has no required methods] is all that is needed. With an immutable class, this is not an option. One option to make the class serializable is to have the class implement externalizable. With the externalizable interface the developer must implement and manage the serialization functions. The second option is to create a serialization proxy, within the immutable’s class. How to create an serialization proxy is beyond the scope of this article.