[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-6351) Enum Bug when using OK word inside of a enum being retrieved
Vlad Yatsenko (JIRA)
noreply at atlassian.com
Fri Nov 25 10:02:19 EST 2011
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-6351?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=44429#comment-44429 ]
Vlad Yatsenko commented on HHH-6351:
------------------------------------
Same is true for Hibernate 3.6.8.Final, and in my case HSQLDB 2.x.x. For an enum property Hibernate creates a column of type CHARACTER which is a fixed length column (contrary to VARCHAR). The length of it probably determined as a length of the longest enum value. When values are read from the table those shorter values come out appended with white spaces. Seems that adding trimming of a String value before converting into an enum type in EnumType.nullSafeGet would fix the problem.
> Enum Bug when using OK word inside of a enum being retrieved
> ------------------------------------------------------------
>
> Key: HHH-6351
> URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6351
> Project: Hibernate Core
> Issue Type: Bug
> Components: annotations
> Affects Versions: 3.3.0.GA, 3.6.5
> Environment: Hibernate version 3.3.0.GA, 3.6.5.Final with hibernate-annotations 3.4.0.GA. Oracle database.
> Reporter: Lucas Arruda
> Priority: Minor
>
> I have a class with a field which is an enum, which is mapped by a enum called Status.
> The class is described (simplified and without all annotations) above:
> public class A {
> private Status status;
> public enum Status { OK, NOK, NAP }
>
> @Column(name = "STATUS")
> @Enumerated(EnumType.STRING)
> public Status getStatus() {
> return status;
> }
> ...
> }
> The thing is. I can store a record from A with status = 'OK', 'NOK' or 'NAP' perfectly. But, when I try to retrieve that record, if status = 'NOK' or 'NAP' it works correctly, but no with status = 'OK', which gives me an exception:
> java.lang.IllegalArgumentException: Unknown name value for enum class com.a.b.c.d.A$Status: OK
> at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:113)
> ...
> Caused by: java.lang.IllegalArgumentException: No enum const class com.a.b.c.d.A$Status.OK
> at java.lang.Enum.valueOf(Enum.java:196)
> at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:110)
> Reported a StackOverflow [1] and now one could solve this using 'OK' in the Enum.
> What I did to solve this, since I couldn't get I done with OK, was to change a little bit the enum with this, which works correctly:
> public enum Status {
> _OK("OK"),
> NOK("NOK"),
> NAP("NAP");
>
> private String desc;
>
> public String getDesc() {
> return desc;
> }
>
> private Status(String desc) {
> this.desc = desc;
> }
> }
> Since I couldn't make it work with 'OK', I highly suspect it's either a big mistake by my part or a bug, so I'm reporting it.
> [1] http://stackoverflow.com/questions/6432933/hibernate-jpa-bug-not-recognizing-some-strings-in-a-enum/
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list