使用HQL查询时提示user is not mapped错误

错误内容:org.hibernate.hql.internal.ast.QuerySyntaxException: user is not mapped [UPDATE user SET token='255387' WHERE id=1]

at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3420)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3309)

原因:HQL找不到 user 表,应该用 实体类名字,查询语句修改下即可。

public void updateToken(int id,String token){
    Transaction tx = null;
    try{
        tx = this.getSession().beginTransaction();
        String sql = String.format("UPDATE UserEntity SET Token='%s' WHERE Id=%s", token,id);
        Query query = this.getSession().createQuery(sql);
        query.executeUpdate();
        tx.commit();
    }catch (Exception ex){
        if(tx!=null)tx.rollback();
        System.out.println("错误信息:");
        ex.printStackTrace();
    }
}

你可能感兴趣的:(使用HQL查询时提示user is not mapped错误)