-->
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.  [ 8 posts ] 
Author Message
 Post subject: Mapping Enum with HBM file
PostPosted: Tue Nov 04, 2008 3:16 am 
Newbie

Joined: Tue Nov 04, 2008 3:07 am
Posts: 9
Till now I used Enums in domain objects with no problem,
Just add the Hibernate annotation:
@Enumerated(EnumType.STRING)

And the domain field saved to the db as selected by the EnumType

Now I am working on different application and trying to use enum in one of my domain object fields

For example:
My domain class is :

Code:
       public class Note{
   private String noteName;
   private NoteType type;
}
Public enum NoteType{ text_note,image_note,reminder_note};



The application uses “hbm” files and I am trying to set the hbm to work with the Enum field (NoteType) so I configured set the hbm like:


Code:
<class name="com.comp.domain.Note" table="app_note" lazy="false">
<property name="type” column="note_type" >
   <type name="EnumUserType">
   <param name="enumClassName">com.comp.enums.NoteType</param>   </type>
</property>



But it wont work, the application keep throwing these exceptions:
“Arithmetic overflow error converting varbinary to data type numeric”

After half day goggling on this matter I found another suggestion :
http://forum.hibernate.org/viewtopic.php?p=2377095

Code:
<property name="myfield" length="30">
   <type name="org.hibernate.type.EnumType">
      <param name="enumClass">org.judge.myenum</param>
      <param name="type">12</param>
   </type>
</property>


this code throws this Exception:
MappingException: "Could not determine type for": org.hibernate.type.EnumType

I published the question in other ORM forum but didn't get an answer..
Hope you guys can help

Sharon


Top
 Profile  
 
 Post subject: You're missing a dependancy
PostPosted: Wed Nov 05, 2008 6:20 pm 
Newbie

Joined: Mon Sep 08, 2008 5:19 pm
Posts: 5
I ran into this, too. The class you're using, org.hibernate.type.EnumType, is not part of the Hibernate Core jar, it's in one of the jars for Hibernate Annotations. So even if you're not using annotations, include the annotation jar files in your classpath, and the XML mapping you have should work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2008 4:55 am 
Newbie

Joined: Tue Nov 04, 2008 3:07 am
Posts: 9
Hi
I added the Hibernate-annotation.jar, indeed the "MappingException" gone
But it still won't accept the hbm settings:


Code:
<property name="myfield"
   <type name="org.hibernate.type.EnumType">
      <param name="enumClass">com.mycomp.modulename.MyEnum</param>
      <param name="type">12</param>
   </type>
</property>


I get ERROR org.hibernate.util.JDBCExceptionReporter - Invalid column name 'myfield'.

If i add getter and setter to access "myfield" that return Integer (its ordinal),Everything is working fine.

Please advice
Thank you
Sharon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 2:41 pm 
Newbie

Joined: Mon Sep 08, 2008 5:19 pm
Posts: 5
Going back to your original post, use this:

Code:
<property name="type" column="note_type" length="10">
   <type name="org.hibernate.type.EnumType">
      <param name="enumClass">com.mycomp.modulename.MyEnum</param>
      <param name="type">12</param>
   </type>
</property>


Your field "type" should be of the enum type, not an Integer.

Two things I should also mention: I use access="field", but I think this will work fine with access="property" as well. Make sure the setter and getter use the enum type, not Integer.

Also, this stores the enum in the database as a String, and the length= attribute specifies how long the string needs to be to hold the longest string representation of the enum. I don't know how to store the enum as an ordinal Integer. Maybe that's controlled by the
Code:
<param name="type">12</param>
line above, and a number other than 12 would mean "integer". But that's just a guess. In my project, I wanted to store Strings, not numbers.

Good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 7:53 am 
Newbie

Joined: Tue Nov 04, 2008 3:07 am
Posts: 9
Sorry for nudging..
I changed the mapping and added the access="field, and also tried access="property"

Also, currently as long as its working... I dont care if its saving string or int ordinal to the database.

My problem is that this Enum Mapping not working,
I keep getting Exception :

Code:
Could not find a getter for myfield in class com.comp.domain.
org.hibernate.PropertyNotFoundException: Could not find a getter for myfield in class com.comp.domain.DomainHib
   at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:282)
   at org.hibernate.property.BasicPropert


I added the Hibernate-annotation.jar to the classpath
change the configuration in the HBM file as following:


Code:
<property access="field" name="myfield" column="my_field" length="30">
         <type name="org.hibernate.type.EnumType">
            <param name="enumClass">com.comp.enums.myfieldsEnum</param>
            <param name="type">12</param>
         </type>
      </property>


I have getter and setter for the Enum in the domain Obj as flow :


Code:
   
  public class DonainObj{

   private  MyfieldsEnum  myfield  ;
                  ....
                  ....

   public MyfieldsEnum  getMyfieldEnum() {
      return myfield;
   }

   public void setMyfieldEnum(MyfieldsEnum  value) {
      myfield = value;
   }
}
   


Please help
Sharon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 11:01 am 
Newbie

Joined: Mon Sep 08, 2008 5:19 pm
Posts: 5
The only thing I see wrong is that the name of the enum class in the XML file doesn't match the full name of your enum type. You have
Code:
MyfieldsEnum
in your entity class, and
Code:
com.comp.enums.myfieldsEnum
in the XML file.

Except for that, it looks like you are doing exactly what I am doing. I'm using Hibernate 3.2.6.GA and Hibernate Annotations 3.3.1.GA. If your versions are different, maybe the problem lies there. I'm new to Hibernate myself, this is my first project. Good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 9:59 am 
Beginner
Beginner

Joined: Tue Jul 03, 2007 11:11 am
Posts: 22
HI grimarr
this is Just a Typo while copying..
the actual xml is Ok

I will be glad to take a peek of your mapping file & Domain Obj

Thank you

Sharon


Top
 Profile  
 
 Post subject: Re: Mapping Enum with HBM file
PostPosted: Thu Jan 26, 2012 9:15 pm 
Newbie

Joined: Thu Jan 26, 2012 6:44 pm
Posts: 1
Check your getter and setter: the field name is myfield and your getter and setter are getMyfieldEnum, setMyfieldEnum


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