-->
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.  [ 10 posts ] 
Author Message
 Post subject: How to access the actual enum value of an Enum UserType?
PostPosted: Fri Aug 12, 2005 5:29 pm 
Newbie

Joined: Fri Jan 07, 2005 3:18 pm
Posts: 16
Location: Boulder, CO
I want to have a property of a Hibernate object map to an enumerated type, and I want to have this type represented in my database table as a VARCHAR. I have implemented a custom UserType class according to the example here: http://www.hibernate.org/265.html This appears to work as expected when I run SchemaExportTask, i.e. the columns are in fact created as type VARCHAR(255). However I can't figure out how I should go about instantiating the Hibernate objects so that they contain the correct enumerated value in this property, since the constructor and property setter methods take an object of the custom UserType as a parameter, rather than of one of the enum type's values.

For example, the constructor for the Hibernate object class that was generated by Hbm2JavaTask looks like this:

Code:
public FruitStandInventory (int quantityField, FruitEnumUserType fruitField);

So if the enum I'm using is something like

Code:
public enum FruitEnum { APPLE, ORANGE, PEAR }

and if I want to instantiate an object with values such as {24, FruitEnum.APPLE} then I can't just use the following code:

Code:
FruitStandInventory fruitStandInventory = new FruitStandInventory (24, FruitEnum.APPLE);

since the second argument to the constructor needs to be of the custom UserType and not the actual enum type that I need to use to set the value.

Also there doesn't appear to be a way to set the value using a property setter method, since it only allows you to set the custom UserType object and not the actual enumeration value being held by the custom UserType property. For example the property setter looks like this:

Code:
public void setFruit (FruitEnumUserType fruit) { this.fruit = fruit; }

so there is no way to set the actual value represented by this field in the database. What I really need is a property setter such as

Code:
public void setFruit (FruitEnum fruit) { this.fruit = fruit; }

but this sort of thing doesn't look to be possible.

Can someone please explain what it is that I'm missing here? Essentially I need to know how to access the underlying enum that is being stored as the object's property, as there doesn't appear to be a way to get at it through the UserType interface.

Also will future versions of Hibernate be able to support simple enum types, as in the example above, without having to resort to creating a custom UserType?

Thanks in advance for any help/insight/suggestions.


--James



Hibernate version: 3.0.5

Mapping documents:see above for example

Code between sessionFactory.openSession() and session.close():I'm using Spring's HibernateTemplate and HibernateDaoSupport classes

Name and version of the database you are using:MySQL


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 12, 2005 6:08 pm 
Newbie

Joined: Mon Jun 06, 2005 11:21 pm
Posts: 10
If you follow the guidelines here

http://www.hibernate.org/272.html

you should be able to map the enum class directly and avoid that problem rather than mapping the custom UserType of the property.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 12, 2005 7:55 pm 
Newbie

Joined: Fri Jan 07, 2005 3:18 pm
Posts: 16
Location: Boulder, CO
I looked at this before and it wasn't obvious how it would help me. I think it makes a little more sense now. If I understand correctly then the point is that the parameter for the ParameterizedType should be the enum that I want to use as the actual type for the property, and then the generated Hibernate object's property will be an enum and not the custom ParameterizedType, allowing me to specify one of the enum values in the constructor and/or property setter. Is my take on this approach correct, or am I still missing the boat?

Unfortunately I haven't been able to get Hbm2JavaTask to generate my Hibernate object classes since the example mapping entries on the page you referenced (the inline type and typedef) aren't valid XML. I have also tried something like the below instead

Code:
        <property name="fruit"
                  column="FRUIT"
                  not-null="true">
            <type name="fruitType"
                  class="com.company.project.usertype.GavinsEnumUserType">
                <param name="enumClassName">com.company.project.usertype.FruitEnum</param>
            </type>
        </property>

but when I run Hbm2JavaTask on the mapping file it gives me the error message

Code:
org.xml.sax.SAXParseException: Attribute "class" must be declared for element type "type".

which doesn't even make sense since the attribute class is in fact declared for the type element.

Any other suggestions?


--James


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 13, 2005 12:19 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It is declared for typedef, not type.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 13, 2005 12:20 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
And Hibernate does not support enums out of the box because of JDK 1.4 backward compatibility

Hibernate Annotations supports them out of the box though

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 13, 2005 3:16 am 
Newbie

Joined: Fri Jan 07, 2005 3:18 pm
Posts: 16
Location: Boulder, CO
emmanuel wrote:
It is declared for typedef, not type.

Can you be a little more specific as to what you're referring to here? What is declared for typedef and not type? On the Wiki page in question there are example mappings for both inline <type> and <typedef> elements, so your response is ambiguous. Perhaps you mean that there is an extraneous </type> in the <typedef> element? Can you please clarify, and perhaps add a little more in the way of an explanation? (I'll give you a credit, although you don't need it!)

On a side note, and I'm probably showing my ignorance here (not a hard thing to do) -- I'm confused by the mixture of single and double quotes in the example mapping elements' XML -- is this good/valid practice? The reason I ask is that I'm used to seeing double quotes in mapping documents XML (and almost all DD's XML for that matter), and I would assume that if you use double quotes for an element's attribute then you would use them for all attributes and not mix the two quote styles, even if it is legal (it's not good style, especially for something which is published as a reference). Or does the single quote have a different meaning to the XML parsers than a double quote? I guess I can try this for myself when I get to work on Monday.

Thanks for reading, even more for responding...


--James


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 13, 2005 3:28 am 
Newbie

Joined: Fri Jan 07, 2005 3:18 pm
Posts: 16
Location: Boulder, CO
emmanuel wrote:
And Hibernate does not support enums out of the box because of JDK 1.4 backward compatibility

This has been made obvious by the various "roll your own" schemes that I've run across, none of which have I been able to fully grasp yet. I did find a comment somewhere mentioning that this was going to be available in Hibernate 3.1 -- is this true?

Quote:
Hibernate Annotations supports them out of the box though

Is there a reference which illustrates this for Hibernate and Annotations newbies? The best I could find was single sentence in this document http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/pdf/hibernate_annotations.pdf which simply states that it does support Enum type mapping out of the box, but with no examples showing how this is done.

Thanks...


--James


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 13, 2005 4:46 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
My answer was done to give you some keywords to the doc, it seems I failed :-)
http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#mapping-types-custom

class is an attribute of typedef, for type use name and put either a defaut hibernate type name, a typedef name or a user type impelmentation fully qualified class name.

Simple quotes are a typo, feel free to fix them. Note, this is not a reference guide but a community wiki, so more subject to errors.

Quote:
I did find a comment somewhere mentioning that this was going to be available in Hibernate 3.1 -- is this true?

No I don't think so.

Quote:
s there a reference which illustrates this for Hibernate and Annotations newbies?

Code:
@Entity public class MyEntity {
    @Id public Integer getId() { ... }
    public MyEnum getMyEnum() { ... }
}

The enum type is automatically mapped either using its ordinal or textual version depending on the underlying DB column type.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 4:45 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Can that automatic enum mapping of hibernate-annotations be used in old school hbm.xml files too? For example by adding the annotations jar in the classpath and maybe configuring something?

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 5:52 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is doable using the EnumType as a custom type, be careful of the needed params though

_________________
Emmanuel


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