开发者

Hibernate Query Language Update Error

开发者 https://www.devze.com 2023-01-26 11:44 出处:网络
Query query = session.createQuery(\"Update Contact set firstname = \'sdf\' where firstname= \'Deepak\'\");
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";

0

精彩评论

暂无评论...
验证码 换一张
取 消