I'm wondering if it's possible to define a map with unknown (Object) element type.
I have an object with the following map defined:
Map<String, Object> attributes = new HashMap<String, Object>();
Sometimes an attribute can be a String
and sometimes it can be a java.util.Date
Is there any elegant way to handle such a data object in Hibernate?
BTW I'm using HBM XML
Thanks!
No.
Just be reasonable. How would you put that into the database? There is no column, that would store whatever you throw at it - that would not be 1NF
Yes.
Well, if you serialize it to a String, and store it in a BLOB, there you go, you can do anything. But that is not an elegant solution, that might pose more problems than it was before. Like you yourself (or an appropriate library) have to implement the conversion to-from the String representation, but to avoid that kind of problems is exactly why you use Hibernate, isn't it?
Conclusion
If a field needs to store a String in one case, and a Date, or a public class Whatchamacallit
instance in another, then it is time to rethink the class hierarchy - you might just need subclasses of a superclass.
Extra
This 'attributes' kind of DB structure has to be used sparingly, and with extra caution! This can lead to very low throughput as the structure gets larger and larger...