Hibernate更新了一定的值

时间:2014-07-23 09:13:17

标签: java hibernate hsqldb

我编写了一个方法,如果它有一个特定的标识符,则选择或更新一个值:

@Transactional
public List<Settings> getSettingsByParameter(String identifier) throws Exception {
    log.info("get resultsList by " + identifier);

    if(identifier.isEmpty()) {
        throw new Exception("Identifier is empty!");
    }

    if(identifier == "today") {
        //update today field
        String query = "UPDATE settings SET value=TODAY() where identifier = '" + identifier + "'";
        em.merge(em.createQuery(query, Settings.class).getSingleResult()); 
    }

    String query = "SELECT p FROM Settings p WHERE identifier = '" + identifier + "'";
    List<Settings> resultList = em.createQuery(query, Settings.class).getResultList(); 

    return resultList;
}

但是,我得到一个例外:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: settings is not mapped [UPDATE settings SET value=TODAY() where identifier = 'today']

为什么以及如何使用hibernate和hsqldb?

我非常感谢你的回答!

1 个答案:

答案 0 :(得分:0)

Hibernate与类名称区分大小写。尝试使用大写S作为Settings类:

String query = "UPDATE Settings SET value=TODAY() where identifier = '" + identifier + "'";