While in the process of creating some basic classes relating do domain objects a problem occurred to me. I know that immutability is quite in vogue at the moment due to its ability to use objects with concurrency without needing locks. Some of the domain objects I was looking at implementing looked like the kind of thing that could be implemented as immutable. However immutable domain classes strike me as not working well or at all with ORM. ORM usually mandates objects with a zero argument constructor and have all the important state available through get/set pairs or at least internal fields inside the class that can be set from the database. An immutable class is really like the other extreme. It would put all the creation state in the constructor because it cannot be changed later. For completeness the internal fields can also be declared final just to make it more obvious if anything would actually change state. I don't see this as something that would be compatible with automatic ORM. It can still be done manually with JDBC. I just wondered if anyone else has considered this same problem and has any thought or insights.
I ended up having two implementations for each domain object or domain object component. One implementation was immutable and the other was mutable. Also I made a method that could be called to convert a mutable object to an immutable version. This would mean in theory that there is a mutable version which plays nicely with ORM and an immutable version which plays nicely with multi threaded contexts. This means though that each domain or domain component needs three source files, a common interface or abstract base class, an immutable implementation and a mutable implementation. This starts to feel like possibly more work that in should be (I wonder if this would get into the topic of other platforms that might be able to declare objects as immutable explicitly in the language avoiding this extra work of doing it by hand). It is useful to sanity check my observations. -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
