java - Hibernate update child from parent, joined column is not specified in generated sql query condition -


parent product entity, , child stock entity distinguished time(like hotel stock model stock of every booking day should different , manipulated separately)

the productentity defined as:

@entity @table(name = "product") public class productentity{      ....      private long productid;      private list<stockentity> stockentitylist;      ....       @id      @column(name="productid")      public long getproductid(){          return this.productid;      }       @onetomany(mappedby="productentity", fetch= fetchtype.eager                   cascade={cascadetype.all,cascadetype.persist,cascadetype.merge},                   orphanremoval = true)      public list<stockentity> getstockentitylist(){                   return this.stockenitylist;      }       public void setstockentitylist(list<stockentity> stockentitylist){...}      .... } 

and stockentity defined as:

@entity @table(name = "stock") public class stockentity{     ...     private productentity productentity;     private long starttime;     ...     @manytoone(fetch=fetchtype.lazy)     @joincolumn(name="productid")     public productentity getproductentity(){          return this.producntentity;     }      @id     @column(name="starttime")     public long getstarttime(){         return this.starttime;     }      .... } 

since aware, use productid of productentity foreign key , starttime(a long type time-stamp) primary key.

then want update specific stock item of product by:

 public void consumestockquantity(long productid, long starttime, int count){      session session = hbsession.getsession();      transaction tx = session.begintransaction();      productentity productentity = session.get(productid, productentity.class);      for(stockentity stockentity: productentity.getstockentitylist()){          if(stockentity.getstarttime() == starttime){             stockentity.setquantity(stockentity.getquantity-count);          }      }      try{         session.update(productentity);         tx.commit();      }catch(exception e){         e.printstacktrace();         tx.rollback();      }finally{         session.close();      }  } 

with above dao code, expect modify stock amount of specific stock item(identified starttime) of specific product.

but times got update count mis-match expected error when there stock items same starttime , different product id, triggered hibernate log , find that, in sql generated hibernate, update sql contains starttime(which explicit @id) no productid in query condition:

hibernate:      update         test.stock      set         marketprice=?,         productid=?,         purchaseprice=?,         quantity=?,         sellprice=?              starttime=? //here missing productid joinedcolumn hibernatelog --> 15:35:27 trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [1] [float] - [1.2] hibernatelog --> 15:35:27 trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [2] [bigint] - [1075] hibernatelog --> 15:35:27 trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [3] [float] - [0.01] hibernatelog --> 15:35:27 trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [4] [bigint] - [1000] hibernatelog --> 15:35:27 trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [5] [float] - [0.01] hibernatelog --> 15:35:27 trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [6] [bigint] - [1438175312412] exception in thread "main" org.hibernate.jdbc.batchedtoomanyrowsaffectedexception: batch update returned unexpected row count update [0]; actual row count: 2; expected: 1 

so, how solve this?

just because productentity join column not make part of id. either you'll have use composite id containing both time stamp , product id (see instance how map composite key hibernate?), perhaps easier not define timestamp id in first place, have normal "autoincrement" or similar id, , have timestamp normal column.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -