如何在 Hibernate 中通过 SQLQuery 执行更新
我需要更新一个加入的子类。由于 Hibernate 不允许更新 hql 或命名查询中的连接子类,因此我想通过 SQL 来完成此操作。我也无法使用 sql 命名查询,因为 Hibernate 不支持通过命名查询进行更新。
所以我决定使用 SQLQuery。但是 Hibernate 抱怨没有调用 addScalar():
org.hibernate.QueryException:在执行查询之前,必须在 SQL 查询上调用 addEntity() 或 addScalar()。
更新是否返回受影响的行数以及该列的命名方式?
还有其他方法可以在休眠中对连接的子类进行更新吗?
这是我正在尝试做的一个简短示例:
<hibernate-mapping>
<class name="example.NiceClass" table="NICE_CLASS">
<meta attribute="class-code"></meta>
<id name="id" type="java.lang.Long">
<column name="NICE_CLASS_ID" precision="8" />
<generator class="sequence">
<param name="sequence">NICE_CLASS_SEQ</param>
</generator>
</id>
</class>
</hibernate-mapping>
<hibernate-mapping package="example">
<joined-subclass name="SubJoinedClass" table="SUB_JOINED_CLASS"
extends="NiceClass">
<key column="NICE_CLASS_ID" foreign-key="NICE_JOINED_ID_FK"/>
<property name="name" type="string" not-null="false">
<column name="NAME" >
<comment>name</comment>
</column>
</property>
</joined-subclass>
</hibernate-mapping>
提前致谢!
所以我想做一个:
update SubJoinedClass set name = 'newName'
I need to update a joined sub-class. Since Hibernate doesn't allow to update joined sub-classes in hql or named-query I want to do it via SQL. I also can't use a sql named-query because updates via named-query are not supported in Hibernate.
So I decided to use a SQLQuery. But Hibernate complaints about not calling addScalar():
org.hibernate.QueryException: addEntity() or addScalar() must be called on a sql query before executing the query.
Are updates returning the number of rows affected and how is named that column?
Are there any other ways to do an update on a joined sub-class in hibernate?
Here is a brief example of what i'm trying to do:
<hibernate-mapping>
<class name="example.NiceClass" table="NICE_CLASS">
<meta attribute="class-code"></meta>
<id name="id" type="java.lang.Long">
<column name="NICE_CLASS_ID" precision="8" />
<generator class="sequence">
<param name="sequence">NICE_CLASS_SEQ</param>
</generator>
</id>
</class>
</hibernate-mapping>
<hibernate-mapping package="example">
<joined-subclass name="SubJoinedClass" table="SUB_JOINED_CLASS"
extends="NiceClass">
<key column="NICE_CLASS_ID" foreign-key="NICE_JOINED_ID_FK"/>
<property name="name" type="string" not-null="false">
<column name="NAME" >
<comment>name</comment>
</column>
</property>
</joined-subclass>
</hibernate-mapping>
Thanks in advance!
So I want to do a:
update SubJoinedClass set name = 'newName'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何执行更新查询?你调用什么方法来执行它?您调用“执行更新”吗?
为什么您认为不能使用 HQL 对子类执行更新查询? AFAIK,这是可能的。
How do you execute the update query ? What method do you call to execute it ? Do you call 'ExecuteUpdate' ?
And why do you think that you cannot execute an update query on a subclass using HQL ? AFAIK, it is possible.