java 如何使用 HQL 将“空”放入列中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1257736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:50:49  来源:igfitidea点击:

How to put 'null' into column using HQL?

javahibernatehql

提问by

How to build valid HQL string, which is equivalent to

如何构建有效的 HQL 字符串,相当于

UPDATE table SET field = null WHERE ....

UPDATE table SET field = null WHERE ....

回答by ChssPly76

Do you mean bulk HQL update? Try this

你的意思是批量 HQL 更新?试试这个

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 documentationfor details.

有关详细信息,请参阅文档

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

如果您不进行批量更新,则应该将您的属性设置为 NULL 并正常保留实体。

回答by Zoidberg

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.

为什么你的update语句需要在HQL中完成?您是否将此表映射到系统中的实体?如果这样做,那么您可以简单地将映射到该列的属性设置为 null,然后对该实体运行保存。例如。

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

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

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

这应该对你有用,但你必须有一个实体映射到有问题的表。