无法 运行 在 Java 中使用 HQL 更新查询
Not able to run update query using HQL in Java
我正在尝试 运行 使用 HQL 更新查询。我觉得我在查询中遗漏了一些东西。因为插入查询工作正常。我的 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>
有什么需要补充的吗?
我见过 JPQL,或者这里可能是 HQL,坚持让实体的大小写与语句中的大小写相匹配。试试 School
.
编辑:这是 Hibernate 文档在 Case Sensitivity.
上所说的(至少是 4.3 版)
尝试在执行查询后提交更改。
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
//your query execution...
tx.commit();
session.close();
我正在尝试 运行 使用 HQL 更新查询。我觉得我在查询中遗漏了一些东西。因为插入查询工作正常。我的 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>
有什么需要补充的吗?
我见过 JPQL,或者这里可能是 HQL,坚持让实体的大小写与语句中的大小写相匹配。试试 School
.
编辑:这是 Hibernate 文档在 Case Sensitivity.
上所说的(至少是 4.3 版)尝试在执行查询后提交更改。
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
//your query execution...
tx.commit();
session.close();