-->
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.  [ 3 posts ] 
Author Message
 Post subject: typesafe enum not implementing PersistentEnum
PostPosted: Tue Nov 25, 2003 4:59 am 
Newbie

Joined: Tue Nov 25, 2003 4:17 am
Posts: 1
Location: Brussels
The great thing about hibernate is that persistent classes do not need to implement special interfaces for persistence...except for Persistent Enum Type (see hibernate doc 4.2.3).
Imagine I have an "Address" object. One of the fields is "Continent". Continent is a typesafe enumeration ={africa, europe,...}
An address can be serialized and sent from the server to a swing client. So the "Address" and "Continent" classes should be unaware of the persistence mechanism...and nothing to do with net.sf.hibernate packages.

According to chapter 4.2.3 of hibernate doc, "Continent" should implement net.sf.hibernate.PersistentEnum. Is it possible to avoid that?

In the mapping document, I don't want to write "Continent" as the typename but rather a "Continenthelper" that wraps "Continent" and implements "PersistentEnum".

Or maybe I should use the "UserType" interface (quoting from source code: "it is a class that knows how to serialize instances of another class to and from JDBC.")

What would be the easiest strategy?
Can someone provide with a fully working example?

Regards,

olivier Roussille


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 8:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes, use a UserType


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 8:39 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
gavin wrote:
Yes, use a UserType


The only drawback in this approach is that you won't be able to write something like:

Code:
Query q = s.createQuery("from Address a where a.continent=:continent");
q.setEnum("continent", Continent.EUROPE);

as you would do with Hibernate enums.

As Continent are not Entities (regarding Hibernate's nomenclature), the only solution seems to be:

Code:
q.setString("continent", Continent.EUROPE.getPersistentValue());

Hence re-introducing a dependency to the persistence. Although this is a minor issue in this case, the Hibernate Enum approach was a nicer solution...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.