How to put 'null' into column using HQL?

穿精又带淫゛_ 提交于 2019-12-30 18:29:50

问题


How to build valid HQL string, which is equivalent to

UPDATE table SET field = null WHERE ....


回答1:


Do you mean bulk HQL update? Try this

UPDATE myEntity e SET e.myProperty = null WHERE ...

You can also use a parameterized version of the above

UPDATE myEntity e SET e.myProperty = :param WHERE ...

In your code:

int updatedEntities = session.createQuery(updateQueryHQL)
  .setString( "param", myValue ) // or .setString( "param", null )
  .executeUpdate();

See documentation for details.

If you're not doing bulk updates, you should just set your property to NULL and persist the entity normally.




回答2:


Why does your update statement need to be done in HQL? Do you have this table mapped to an entity in the system? If you do, then you can simply set the property that maps to that column to null, and run a save on that entity. eg.

myObject.setMyProperty(null); getSessionFactory().getCurrentSession().save(myObject);

That should work for you, but you gotta have an entity mapped to the table in question.



来源:https://stackoverflow.com/questions/1257736/how-to-put-null-into-column-using-hql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!