java - OptimisticLockException Ebean even with @Version -
i tried update row in db using ebean in play! framework program. 
 here class of entity update. 
 
 transaction.java
@entity @table(name = "transactions") public class transaction extends model{      @id     @generatedvalue     public int id;      @onetoone     @joincolumn(name = "car_fk")     public car car;      @onetoone     @joincolumn(name = "user_lender_fk")     public user user;      @version     public timestamp from_date;      @version                         public timestamp to_date;       public boolean availability; // true -> available.      public string status;  }   and here metho use update it:
transaction transaction = new transaction();  transaction.car = concernedcars.get(i);  transaction.user = currentuser;  transaction.from_date = tools.stringandroidtotimestamp(datefrom);  transaction.to_date = tools.stringandroidtotimestamp(dateto);  transaction.status = constants.waiting_for_answer;  try{     ebean.update(transaction);  }catch(optimisticlockexception e){     logger.info(e.tostring()); }   and if necessary, method convert string timestamp:
public static timestamp stringandroidtotimestamp(string s){         string toconvert = s.substring(0, s.length()-2);          logger.info("toconvert = "+toconvert);         timestamp timestamp = null;          try{             simpledateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");             date parseddate = dateformat.parse(toconvert);             timestamp = new timestamp(parseddate.gettime());         }catch(exception e){             logger.info("exception date = " +e.tostring());         }         return timestamp;     }   of course, fabulous error:
javax.persistence.optimisticlockexception: data has changed. updated [0] rows sql
what did wrong?
there few ways can handle this.
1) use @entityconcurrencymode(concurrencymode.none) before class name
2) use raw update query.(preferred)
i facing problem because of same ebean.update throwing optimistic lock exception, did raw update query , worked me.
Comments
Post a Comment