Hibernate Query Language Update Error
Query query = session.createQuery("Update Contact set firstname = 'sdf' where firstname= 'Deepak'");
int result = query.executeUpate();
when executing the above query, I am getting the following error
expecting DOT, found '=' near line 1, column 30
[Update Contact set firstname = 'sdf' where firstname= 'Deepak']
Can some开发者_JAVA百科one please help me with this
You are writing the query in SQL, Not HQL. A bulk update in HQL would look something like this:
String query = "update Contact c set c.firstname = :newName where c.name = :oldName";
int result = s.createQuery( query )
.setString( "newName", "sdf")
.setString( "oldName", "Deepak" )
.executeUpdate();
However, unless there are lots of rows with firstname deepak, this is the wrong approach. If you are just updating one object, it's better to a) do a query which returns this object, and b) update the object just by myobject.firstname = "sdf";
精彩评论