1

I'm modifying an exising POJO to include a boolean field, and want to display said Categories based on the boolean value.

To do so, I have updated the Hibernate XML configuration and added the fields to the database. My issue is that a HibernateQueryError is thrown on the Criteria.list() method when returning the data because Hibernate cannot resolve the new field.

Category POJO:

public class Category implements java.io.Serializable {

private long id;
private String name;
private Short displayOrder;
private Boolean enabled;
private Set<Submission> submissions = new HashSet<Submission>(0);

   //getters and setters
   public Boolean getEnabled(){
      return this.enabled;
   }

   public void setEnabled(Boolean enabled){
      this.enabled = enabled;
   }

Added mapping to hibernate config:

<hibernate-mapping>
    <class name="com.jason.app.hibernate.Category" table="CATEGORY" lazy="false">
    ... other properties

    <property name="enabled" type = "java.lang.Boolean">
        <column name="IS_ENABLED" />
    </property>

    ... set mapping configuration

</hibernate-mapping>

and this is the trouble spot:

private boolean hasDisplayOrder;
private boolean isEnabled;

public List<V> list() {

    try {
        Criteria criteria = getSession().createCriteria(returnedClass());

        if (hasDisplayOrder && isEnabled){
            criteria = criteria.add(Restrictions.eq("enabled", true))
                                .addOrder(Order.asc("displayOrder"));
            log.info("Category object is enabled, added to criteria");
        }
        else if (hasDisplayOrder ) {
            criteria = criteria.addOrder(Order.asc("displayOrder"));
            log.info("Object does not contain enabled method");
        }

        return criteria.list();
    }
    catch (RuntimeException re) {
        log.error("Could not retrieve list results for class "
                + returnedClass.getSimpleName(), re);
    }

    return null;
}

The logging statement inside the first conditional triggers in the logs, and then an error message is printed out.

2014-02-12 12:02:49,482 INFO  [com.jason.app.manager.BaseManager]  Category object is enabled, added to criteria

2014-02-12 12:02:49,489 ERROR [com.jason.app.manager.BaseManager] Could not retrieve list results for class Category
org.hibernate.QueryException: could not resolve property: enabled of: com.jason.app.hibernate.Category

Things I've tried:

  • Change data type from boolean to Boolean in Category
  • change type from boolean to java.lang.Boolean in hibernate config

The web form that this POJO is used for displays just fine if I remove the conditional inside the list() method above, but it shows all categories regardless of state.

Are there any other options to try out?

Jason
  • 11,263
  • 21
  • 87
  • 181
  • Please tell me the package of this Category class.I know in logs it is showing com.jason.app.hibernate .But i need to be sure – Kumar Abhinav Feb 12 '14 at 17:34
  • It is. That's where the domain objects reside along with their corresponding CLASSNAME.hbm.xml mappings – Jason Feb 12 '14 at 17:36
  • Did you recently add this column in your database.Please check if the column name is spelled correctly – Kumar Abhinav Feb 12 '14 at 17:46
  • Column name is correct in the xml. The field was recently added into the oracle db as a `Number(1)` and contains either a 1 or zero. – Jason Feb 12 '14 at 17:59
  • 1
    Change your `getEnabled()` to `isEnabled()` and I would use a `boolean` instead of a `Boolean` (unless the field can actually be `null`). Boolean fields don't have a `get*` method the have an `is*` method. See http://stackoverflow.com/questions/799280/valid-java-bean-names-for-booleans – M. Deinum Feb 12 '14 at 18:03

1 Answers1

0

What if you switched your Java code to use Boolean instead boolean, something like this:

private boolean enabled = true;

public boolean isEnabled() {
    return enabled;
}

public void setEnabled(boolean enabled) {
    this.enabled = enabled;
}
SergeyB
  • 9,478
  • 4
  • 33
  • 47
  • Already tried it, along with `getEnabled()`, both with primitive and object. Nothing worked. – Jason Feb 14 '14 at 00:42