无法使用Java中的HQL运行更新查询

时间:2016-02-22 05:07:07

标签: java hibernate hql

我尝试使用HQL运行更新查询。我觉得我在查询中遗漏了一些东西。因为Insert查询工作正常。我的HQL查询是

Query query = session.createQuery("update school set dbvalue = :dbvalue" +
                    " where nameValue= :nameValue and uid = :uid");
            query.setParameter("dbvalue", dashBoardValue);
            query.setParameter("nameValue ", nameValue );
            query.setParameter("uid", userId);

            int rowsAffected = query.executeUpdate();

我得到的错误是

org.hibernate.hql.internal.ast.QuerySyntaxException: school is not mapped [update school set dbvalue = :dbvalue where nameValue = :nameValue and uid = :uid]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)

我的hibernate.cfg.xml是

<?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/intu</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.cni.school" />
</session-factory>
</hibernate-configuration>

有什么需要添加的吗?

2 个答案:

答案 0 :(得分:1)

我已经看到JPQL,或者也许是HQL,坚持让实体的案例与声明中的案例相匹配。试试School

编辑:这是Hibernate文档在Case Sensitivity上所说的(至少版本4.3)。

答案 1 :(得分:0)

尝试在执行查询后提交更改。

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

   //your query execution...

tx.commit();
session.close();