|
|
Problem with enum and hibernate!
hi all!
i followed this solution 272.html to add typsafe generic enums in my project.
This is my enum class:
public enum UnterrichtsDauerExtended implements Serializable {
Unknown(0),
GROESSER_GLEICH_60(1),
GROESSER_GLEICH_90(2);
int value;
private UnterrichtsDauerExtended(int value) {
this.value = value;
}
/*** @return*/
public int getValue() {
return value;
}
/*** @see java.lang.Enum#toString()*/
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
/*** @param value* @return*/
public static UnterrichtsDauerExtended getInstanceByValue(Integer value) {
switch (value) {
case 1:
return GROESSER_GLEICH_60;
case 2:
return GROESSER_GLEICH_90;
default:
return Unknown;
}
}
and this are the important parts of my mapping file:
lt;typedef name=quot;UnterrichtsDauerExtendedquot; class=quot;najsre7.dao.hibernate.userType.immutable.Ge nericEnumUserTypequot;gt; lt;param name=quot;enumClassquot;gt;najsre7.model.immutable.kurs.Unte rrichtsDauerExtendedlt;/paramgt; lt;param name=quot;identifierMethodquot;gt;getValuelt;/paramgt; lt;param name=quot;valueOfMethodquot;gt;getInstanceByValuelt;/paramgt; lt;/typedefgt;
lt;subclass name=quot;najsre7.model.KursNutzergruppe1quot; discriminator-value=quot;1quot;gt;
lt;property name=quot;kursArtquot; type=quot;najsre7.dao.hibernate.userType.immutable.kur s.KursArtUTquot;gt;lt;meta attribute=quot;use-in-tostringquot;gt;truelt;/metagt;lt;meta attribute=quot;use-in-equalsquot;gt;truelt;/metagt;lt;column name=quot;SIPAR1quot; precision=quot;5quot; scale=quot;0quot; not-null=quot;truequot; /gt; lt;/propertygt; lt;property name=quot;unterrichtsDauerquot; type=quot;UnterrichtsDauerExtendedquot;gt;lt;meta attribute=quot;use-in-tostringquot;gt;truelt;/metagt;lt;meta attribute=quot;use-in-equalsquot;gt;truelt;/metagt;lt;column name=quot;SIPAR2quot; precision=quot;5quot; scale=quot;0quot; /gt; lt;/propertygt;
//more
But now i'm not able to save an object of type KursNutzergruppe1! If i call the save method of the DAO, somewhere the following exception occurs:
java.lang.NoSuchMethodError: najsre7.model.immutable.kurs.UnterrichtsDauerExten ded.intValue()I
I'm not sure if this is a spring or a hibernate problem :-/ Why is it calling the intValue() method? (sorry but i'm really new into these enums)
These are the DEBUG log messages i see:
Hibernate: insert into TKURS (DMUTDAT, FK_TANGE_ILAUFNR, FK_TSPA_ILAUFNR, SGRUPPE, ISTATUS, DDATUM_VON, DDATUM_BIS, SMUTUSER, SSTVUSER, SIPAR1, SIPAR2, SIPAR3, SIPAR4, SIPAR5, SIPAR6, IMENGE1, IMENGE2, IMENGE3, IMENGE4, IMENGE5, IMENGE6, IMENGE7, IMENGE8, IMENGE9, ILAUFNUMMER) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2006-08-31 08:02:51,798 DEBUG binding '2006-08-31 08:02:51' to parameter: 1 in (NullableType.java:80)
2006-08-31 08:02:51,798 DEBUG binding '7' to parameter: 2 in (NullableType.java:80)
2006-08-31 08:02:51,798 DEBUG binding '35' to parameter: 3 in (NullableType.java:80)
2006-08-31 08:02:51,798 DEBUG binding 'BLKURS' to parameter: 4 in (NullableType.java:80)
2006-08-31 08:02:51,798 DEBUG binding '1' to parameter: 5 in (NullableType.java:80)
2006-08-31 08:02:51,798 DEBUG binding '01 Januar 2006' to parameter: 6 in (NullableType.java:80)
2006-08-31 08:02:51,798 DEBUG binding '29 August 2006' to parameter: 7 in (NullableType.java:80)
2006-08-31 08:02:51,798 DEBUG binding 'admin' to parameter: 8 in (NullableType.java:80)
2006-08-31 08:02:51,798 DEBUG binding null to parameter: 9 in (NullableType.java:73)
2006-08-31 08:02:51,798 DEBUG binding '1' to parameter: 10 in (NullableType.java:80)
Any ideas?
Kind regards
angela
Not sure but I think your identifierMethod should return the same value as your valueOfMethod, ie. both should deal with Integer or int.
Why is it calling the intValue() method? (sorry but i'm really new into these enums)
Enums are internally stored as integers so to get the value of an enum, the int value is required.
There are some UserTypes for enums inside the HB faq - take a look on their site for more info. As a note, Spring doesn't intervene in any way with Hibernate when it comes to UserTypes.
Originally Posted by Costin LeauEnums are internally stored as integers so to get the value of an enum, the int value is required.
There are some UserTypes for enums inside the HB faq - take a look on their site for more info. As a note, Spring doesn't intervene in any way with Hibernate when it comes to UserTypes.
thanks for the helpful answer! regards angela |
|