3 Replies Latest reply on Feb 22, 2007 11:10 AM by alexg79

    HQL and enum constants

    alexg79

      According to the Hibernate manual, it is possible to use public static final constants directly in HQL queries.
      Why then can I not use enums there as well?
      I tried

      SELECT DISTINCT m FROM Material m, IN(m.suppliers) s WHERE m.unit <> Material.MaterialUnit.SQUARE_METRES AND s.id = :id
      

      and variations like "MaterialUnit.SQUARE_METRES" and the fully qualified name, but they all produce the same error message on deployment:

      13:55:36,982 ERROR [SessionFactoryImpl] Error in named query: Material.nonCuttables
      org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'null.MaterialUnit.SQUARE_METRES' [SELECT DISTINCT m FROM fi.karico.etikettu.domain.Material m, IN(m.suppliers) s WHERE m.unit <> Material.MaterialUnit.SQUARE_METRES AND s.id = :id]

      The same works fine if I just input the enum as a parameter:
      SELECT DISTINCT m FROM Material m, IN(m.suppliers) s WHERE m.unit <> :m2unit AND s.id = :id
      

      What am I doing wrong?

        • 1. Re: HQL and enum constants
          andydale

          Hi.

          try this:

          SELECT DISTINCT m FROM Material m, IN(m.suppliers) s WHERE m.unit <> Material$MaterialUnit.SQUARE_METRES AND s.id = :id


          I am not sure if you will need the package name before Material.

          Cheers,

          Andy

          • 2. Re: HQL and enum constants
            alexg79

            Tried that, and "com.foo.Material$MaterialUnit.SQUARE_METRES", no dice. Gives the same error still.

            • 3. Re: HQL and enum constants
              alexg79

              For the record, here's the code for the MaterialUnit enum inside the Material class:

               public enum MaterialUnit {
               SQUARE_METRES("m\u00b2"),
               ROLLS("rll"),
               PIECES("kpl"),
               KILOGRAMS("kg");
              
               private final String name;
              
               private MaterialUnit(final String name) {
               this.name = name;
               }
              
               @Override
               public String toString() {
               return name;
               }
              
               }