尝试做Hibernate更新 - 有人能指出我正确的方向吗?

时间:2012-02-08 00:01:39

标签: hibernate

我试图按照文档中的示例进行操作 http://docs.jboss.org/hibernate/orm/3.3 ... tch-direct

但是在我的服务器日志中出现以下错误:

  

SEVERE:org.hibernate.QueryException:查询必须以SELECT或FROM开头:update [update hibernate.TbUser u set u.vcUserName =:userName,u.vcUserPsw =:userPsw,u.vcUserFirstName =:firstName ,u.vcUserLastName =:lastName,u.vcUserMiddleName =:middleName,u.vcEmailAddress =:email,u.vcPhoneNumber =:phone,u.dtTerminatedDate =:termDate,u.dtHireDate =:hireDate,u.dtUpdTime =:updTime, u.iUpdUserId =:userId,其中iUserId =:id]

这表示我需要使用SELECTFROM启动查询,但该示例在更新查询中使用Update,因此我不明白问题所在。

有谁能告诉我一个如何做Update ???的例子? 文件显然没有。 感谢。

以下是我要执行的代码:

public int updateUser( String userName, String passwd, String firstName, String middleName, 
        String lastName, String email, String phone, Date termDate, Date hireDate, int id )
{
    int status = 0;
    try {
        org.hibernate.Transaction tx = session.beginTransaction();
        String cmd = "update TbUser u set u.vcUserName = :userName, "+
                "u.vcUserPsw = :userPsw, u.vcUserFirstName = :firstName, "+
                "u.vcUserLastName = :lastName, u.vcUserMiddleName = :middleName, "+
                "u.vcEmailAddress = :email, u.vcPhoneNumber = :phone, "+
                "u.dtTerminatedDate = :termDate, u.dtHireDate = :hireDate, "+
                "u.dtUpdTime = :updTime, u.iUpdUserId = :userId where iUserId = :id";
        Query q = session.createQuery( cmd );
        q.setString("userName", userName);
        q.setString("userPsw", passwd);
        q.setString("firstName", firstName);
        q.setString("middleName", middleName);
        q.setString("lastName", lastName);
        q.setString("email", email);
        q.setString("phone", phone);
        q.setDate("termDate", termDate);
        q.setDate("hireDate", hireDate);
        q.setDate("updTime", new Date());
        q.setInteger("id", id);
        status = q.executeUpdate();
        tx.commit();
    }
    catch( Exception e ) {
        e.printStackTrace();
        status = -1;
    }
    return status;
}

1 个答案:

答案 0 :(得分:0)

查看此页面here,看起来您需要在类路径中包含antlr * .jar并使用其他解析器org.hibernate.hql.ast.ASTQueryTranslatorFactory

尝试使用该库,看看你如何去。