Hibernate - 访问新数据字段时无法解决属性错误

时间:2014-02-12 17:21:48

标签: java spring hibernate hibernate-mapping

我正在修改一个exising POJO以包含一个布尔字段,并希望根据布尔值显示所述类别。

为此,我更新了Hibernate XML配置并将字段添加到数据库中。我的问题是,在返回数据时,Criteria.list()方法会抛出HibernateQueryError,因为Hibernate无法解析新字段。

类别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;
   }

添加了映射到hibernate配置:

<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>

这就是问题所在:

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;
}

日志中第一个条件触发器内的日志记录语句,然后打印出一条错误消息。

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

我尝试过的事情:

  • Category
  • 中将数据类型从布尔值更改为布尔值
  • 在hibernate config
  • 中将类型从boolean更改为java.lang.Boolean

如果我在上面的list()方法中删除了条件,那么此POJO用于​​的Web表单就会显示正常,但它会显示所有类别,无论状态如何。

还有其他选择吗?

1 个答案:

答案 0 :(得分:0)

如果您将Java代码改为使用Boolean而不是boolean,那会怎么样:

private boolean enabled = true;

public boolean isEnabled() {
    return enabled;
}

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