English 中文(简体)
休眠更新不工作
原标题:Hibernate update not working

我是新来的冬眠者,我正试图用以下代码更新一个已绘制的物体,但它没有更新

factory = config.buildSessionFactory();
session = factory.getCurrentSession();
Transaction t = session.beginTransaction();
String hql = "UPDATE  "+tableName+" SET  "+columnName+" =   "+columnValue+"  WHERE  id ="+id+";";
Query query=session.createSQLQuery(hql);
t.commit();

我是不是漏掉了什么? 它不会坠毁,也不会更新记录。

注:我使用休眠3和弥斯克尔

最佳回答

您再次丢失了 query. executeUnited () ;

此外,如果您重新更新一个已映射的天体, 我建议您对 java 天体进行修改, 让 Hibernate 为您进行更新。 或者至少使用 hql 查询, 而不是本地查询 。

确保您的持久性. xml 文件显示_ sql 设置为真实, 并观看日志以查看是否执行更新 。

<property name="hibernate.show_sql" value="true"/>
问题回答

You need to use query.executeUpdate() to run the query.
Also it is suggested that you use parameters instead of inline arguments. For eg. if the columnName = O Reilly then the whole query will go wrong. Also if it is a mapped object you can use HQL rather than SQL query

相反,您可以使用此工具

//entity is your hibernate entity obj
String hql = "UPDATE  " + entity.getClass().getName + " as entity SET entity." + fieldName  + "= :columnValue WHERE  entity = :entity";
Query query=session.createQuery(hql).setParameter("columnValue", columnValue).setParameter("entity", entity);
query.executeUpdate();

注意您不需要使用单引号。 setParameter 处理它。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签