-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: UserType question
PostPosted: Tue Jul 06, 2004 1:55 pm 
Beginner
Beginner

Joined: Thu Apr 15, 2004 5:12 pm
Posts: 21
I'm still a little confused about UserType, and when it is necessary, and how to implement it, and am hoping somebody could just scan the following and see if I'm on the right track.

If I have the following class:

Code:
public class PropertiesWrapper {
    private String id;
    private java.util.Properties properties;
    // constructors and delegate methods omitted
}
// and there is a helper class that implements UserType elsewhere


Is creating my own helper class that implements UserType the easiest way of persisting objects of this type, or am I missing an easier solution?

If that is the simplest solution, does the following look like the correct mapping for this class?

Code:
<hibernate-mapping>
    <class name="com.foobar.PropertiesWrapperType" 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="java.util.Properties">
            <column name="properties" sql-type="VARCHAR(1000)" not-null="true"/>
            <!-- assuming very small properties files -->
        </property>
      </class>
</hibernate-mapping>


thanks very much...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 6:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
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.


Top
 Profile  
 
 Post subject: thanks
PostPosted: Tue Jul 06, 2004 7:23 pm 
Beginner
Beginner

Joined: Thu Apr 15, 2004 5:12 pm
Posts: 21
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....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 7:36 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
If in the future you change the code for the class you have serialized (say add some fields), the old serialized objects in the database may not deserialize, unless you take specific measures to ensure that all future versions of your class can deserialize old version (in general this is a non-trivial thing to do).

Querying on the properties of a serialized object in the database is not possible (as far as I know).

A serialized object stored in the database cannot be used by a non-Java program.

These are just a few of the possible gotchas...

In your case, I believe java.util.Properties serializes into a form that is pretty future proof, so it just may work depending on your needs. But, I'd still say no if one of my developers tried to do that, as a matter of principle.

--gus


Top
 Profile  
 
 Post subject: thanks
PostPosted: Wed Jul 07, 2004 6:00 pm 
Beginner
Beginner

Joined: Thu Apr 15, 2004 5:12 pm
Posts: 21
gus wrote:
If in the future you change the code for the class you have serialized (say add some fields), the old serialized objects in the database may not deserialize, unless you take specific measures to ensure that all future versions of your class can deserialize old version (in general this is a non-trivial thing to do).

Querying on the properties of a serialized object in the database is not possible (as far as I know).

A serialized object stored in the database cannot be used by a non-Java program.

These are just a few of the possible gotchas...

In your case, I believe java.util.Properties serializes into a form that is pretty future proof, so it just may work depending on your needs. But, I'd still say no if one of my developers tried to do that, as a matter of principle.

--gus


Thanks for the explanation, Gus. they're all very good points, and good reasons to define a UserType. In this case, I don't need to query the database on anything but id, so that isn't a concern, but it would certainly be better to persist as varchar using the standard Properties.store() format so that there is no Java or class-version dependency.

cheers..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.