java - Hibernate UPDATE Query setting other values to NULL -


i having following table storing blog posts in mysql:

+-----------+--------------+------+-----+---------+----------------+ | field     | type         | null | key | default |          | +-----------+--------------+------+-----+---------+----------------+ | postid    | int(11)      | no   | pri | null    | auto_increment | | posttitle | varchar(250) | yes  |     | null    |                | | postdate  | datetime     | yes  |     | null    |                | | ownerid   | int(11)      | yes  | mul | null    |                | | postvote  | int(11)      | yes  |     | null    |                | +-----------+--------------+------+-----+---------+----------------+ 

i want use hibernate update query update postvote.

here java method same:

@put @consumes({ mediatype.application_json }) public void updatevotes (posts p) { session ses = hibernateutil.currentsession(); transaction tx = null; try{     tx =ses.begintransaction();     ses.createquery("update posts set postvote="+p.getpostvote() +" postid="+p.getpostid()+" , ownerid="+p.getownerid());     ses.update(p);     tx.commit();     }catch (hibernateexception e) {     if (tx!=null) tx.rollback();     e.printstacktrace();  }finally {      hibernateutil.closesession();   } } 

i calling method 3 parameters in posts p (post id,postvote,ownerid), ones need query. once query executed, other values set null except 3 values. here hibernate query output:

hibernate: update posts set posttitle=?, postdate=?, ownerid=?, postvote=? postid=? 

which doesn't appear fine seems update unwanted elements. advise wrong in actual query?

use dynamic-update=true

check link may helpful you

http://www.mkyong.com/hibernate/hibernate-dynamic-update-attribute-example/


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -