10 Replies Latest reply on Feb 22, 2006 7:36 AM by Jens Elkner

    Hibernate enum bug ?

    Jens Elkner Apprentice

      I have an entity bean, which uses an inner public enum class:

      @Entity
      @Table(name="events")
      public class Event implements Serializable {
      ...
       private DateType datetype = DateType.NONE;
      
       public enum DateType {
       /** not set */
       NONE,
       /** date, only */
       DATE,
       /** time, only */
       TIME,
       /** date and time */
       DATETIME
       }
      
       @Column(name="datetype")
       public DateType getDatetype() {
       return datetype;
       }
      ...


      Specs say, that enums may be stored as strings or integers - hibernate uses Intergers - great!
      But when I create a query with a parameter, which refers to a datetype, hibernate seems to use the String instead of the ordinal value. E.g.

      em.createQuery("FROM Event WHERE datetype=:dt1 OR datetype=:dt2")
       .setParameter("dt1", DateType.DATE)
       .setParameter("dt2", DateType.DATETIME)
       .getResultList();


      Always returns an empty list, even so there are entities with datetype== DateType.DATETIME.

      So do I something wrong here, or is it just a problem of hibernate, that it does not know anymore, how it has updated the db schema? Are there any annotations to give hybernate a hint, to get the thing right ?

        • 1. Re: Hibernate enum bug ?
          Emmanuel Bernard Master

          use enum.ordinal() for now in the setParameter(),
          This is going to be addressed in the spec

          • 2. Re: Hibernate enum bug ?
            Emmanuel Bernard Master

            FYI, Hibernate does not choose numeric, it checks the column to guess the type. Of course if you create the schema from Hibernate this is not possible

            • 4. Re: Hibernate enum bug ?
              Simon Newbie

              Any more info on the spec situation regarding this? I didn't find anything in PFD spec, but I guess it was released too soon to have the issue resolved by then.

              • 5. Re: Hibernate enum bug ?
                Jens Elkner Apprentice

                see "9.1.19 Enumerated Values" in the Java Persistence API PFD

                • 6. Re: Hibernate enum bug ?
                  Simon Newbie

                  My enum mappings work fine except for when used as parameters in EJB-QL queries, so I guess I've got a completely different problem, though symptoms looked the same when I searched the forum. I misread the comments below is thinking that it was EJB-QL issues that would be addressed.

                  Looks like I get a serialized enum stuffed into the parameter, rather than the ordinal or name as annotation declares. This from the MySQL query log, at the point the parameter is inserted:

                  '’\0~r\0\'example.MyEnum\0\0\0\0\0\0\0\0\0\0xr\0java.lang.Enum\0\0\0\0\0\0\0\0\0\0xpt\0\rTEST_ENUM_TYPE'

                  I'm using JBoss 4.0.4RC1, MySQL 5.0.18, and MySQL-connector 3.1.12. Use of ordinal or name methods work fine though of course. Any ideas?

                  • 7. Re: Hibernate enum bug ?
                    Jens Elkner Apprentice

                    This was a problem with 4.0.3SP1 (EJB RC3) - one had to use enumvar.ordinal(). The ordinal stuff however does not work anymore with 4.0.4RC1. So I removed .ordinal() from all parameters and it works as expected.

                    So at least for me everything is fine with enums now (using MySQL 5.0.15, connector 3.1.12).

                    Are you sure, that you are using EJB RC5 and the related recent hibernate libs ?

                    • 8. Re: Hibernate enum bug ?
                      Simon Newbie

                      I used the installer for 4.0.4RC1, and selected the ejb3 option during the install process, which should give me RC5. I've also tried updating the hibernate and EJB3 libs manually to latest versions, and still get the same problem. Files inside my 4.0.4RC1 installer are stamped 07/02/06 21:24

                      Hmmm...I wonder what could be causing my problem. I'm using field annotations, named parameters, and enums defined in seperate files, but can't imagine those would be factors. Tinyint as my db datatype? Nah. I'm not sure what to consider next at this point, as it seems all should be working (and does outside of EJB-QL). However, it's good to know that enums are supported now in the way I hoped they'd be, and thanks for helping.

                      • 9. Re: Hibernate enum bug ?
                        Simon Newbie

                        The other thing I'm using, which I forgot to mention, is named queries declared at class level. Anybody else had success/failure with enums in EJB-QL?

                        • 10. Re: Hibernate enum bug ?
                          Jens Elkner Apprentice

                          Just to make sure: your enums are not annoted with @Embedded or something like that, are they? For a shot into the blue, just remove all annotations from your enum and check, whether it works...