michael wrote:
The mapping should be like
Code:
<hibernate-mapping>
<class name="com.foobar.PropertiesWrapper" table="properties">
<id name="id" type="string">
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="properties" type="com.foobar.PropertiesType">
<column name="properties" sql-type="VARCHAR(1000)" not-null="true"/>
<!-- assuming very small properties files -->
</property>
</class>
</hibernate-mapping>
Where PropertiesType is you helper UserType which knows how to persist the properties into a database field.
Thanks very much for the help, Michael. I was thinking the UserType was responsible for persisting the whole object, but I see now that it's just responsible for persisting an object field to a database field, which makes sense now that I think about it.
I've also just discovered an easier solution, for anybody else reading this thread later. The following mapping works fine without needing to create a UserType at all:
Code:
<hibernate-mapping>
<class name="com.foobar.PropertiesWrapper" table="properties">
<id name="id" type="string">
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="properties" type="serializable">
<column name="properties" sql-type="VARBINARY(1000)" not-null="true"/>
</property>
</class>
</hibernate-mapping>
The difference is that I defined the database column as VARBINARY instead of VARCHAR, and I declared the hibernate type as serializable. Hibernate seems to take care of everything else required, as long as the object's class implements java.io.Serializable (which java.util.Properties, like most Java classes, does).
I asked another question once before about persisting an org.dom4j.Document, and was told to use UserType by a Hibernate developer. I'm curious why I haven't seen the serializable approach recommended. Is there some big limitation to the approach above, or some reason why for simple serializable classes it is better to do it via a user type than just to declare it as serializable? Not defining a UserType for every custom class is much less work, but maybe I'm overlooking something.
thanks....