How to put "null" in a column using HQL?

How to create a valid HQL string equivalent to

Table UPDATE SET field = null WHERE ....

+7
java hibernate hql
source share
2 answers

Do you mean HQL bulk update? try it

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

You can also use the parameterized version 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 the documentation for more details.

If you are not doing bulk updates, you should simply set your property to NULL and save the object in normal mode.

+2
source share

Why should your update statement run in HQL? Do you have this table mapped to an entity in the system? If you do this, you can simply set the property that maps this column to null and start saving this object. eg.

myObject.setMyProperty (zero); . GetSessionFactory () getCurrentSession (), with the exception of (MyObject).

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

0
source share

All Articles