3 Replies Latest reply on Feb 2, 2011 8:20 AM by wdfink

    Using enum "fields" in hql

    newbeewan

      Hi,

       

      I have this enum :

       

      public enum ValidationState {
            /**
             * Waiting for validation
             */
            PENDING(true),
            /**
             * Validated but cancellable (not taken yet !)
             */
            VALIDATED(true),
            /**
             * Canceled period
             */
            CANCELED,
            /**
             * Validated and uncancellable (date is over)
             */
            VALIDATE_CLOSED;
      
            private final boolean cancelable;
      
            private ValidationState(){
                  this(false);
            }
      
            private ValidationState(boolean cancelable){
                  this.cancelable = cancelable;
            }
      
            public boolean isCancelable(){
                  return this.cancelable;
            }
      
      }
      

       

      I'm using it in an entity :

       

      @Column(name="VALIDATION_STATE")
            @Enumerated(EnumType.STRING)
            private ValidationState state;
      

       

      I want to select some entities via HQL using the value of the cancelable property

      I have created that request

       

      FROM ThePeriod period WHERE period.uid = :uid AND period.state.cancelable = true
      

       

      I've got a QueryException :

       

      org.hibernate.QueryException: could not resolve property: cancelable of: business.data.ThePeriod [FROM ThePeriod period WHERE period.uid = :uid AND period.state.cancelable = true ]
              at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
              at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
              at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1449)
              at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:315)
              at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:487)
              at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:611)
              at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:263)
              at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:210)
      

       

      Is it possible to use the value of an enum's property into an HQL query ?

       

      I'm using hibernate 3.6.0 as entity manager.

       

      Regards

        • 1. Using enum "fields" in hql
          wdfink

          I'm not really sure but I think it is impossible to access the enum-object properties within finders.

          This will only work if you create a table where the state is stored with a column cancelable.

          • 2. Using enum "fields" in hql
            newbeewan

            I think so !

             

            I don't know if it is possible to make an enum serialized into a table... If not, I need to create a new entity and populate some referencial data manually... it is not very usefull !

            • 3. Using enum "fields" in hql
              wdfink

              I think a serialzed enum within the database will not solve your problem, you must read it and check cancelable afterwards.

              This is the same as @Enumerated via entity manager.