hibernate - Insert persistence record in DB where two table are seprate -
i using struts
, hibernate
. trying insert data in 2 table independent. if first table insertions create error can not able insert second table data , vice-versa. want this. how can this?
code
public string create() { getdaofactory().instantiatedao(daoclass).saveorupdate(entity); getdaofactory().getmatchpredictionoptionlevel2().saveorupdate( optionlevel2); return success: } public t saveorupdate(t entity) { try { getsessiontx().save(entity); commit(); } catch (exception e) { rollback(); e.printstacktrace(); } { getsession().close(); } return entity; } public session getsessiontx() { system.out.println("in sassion method getsessiontx"); if (this.session == null) this.session = hibernateutil.getsessionfactory().opensession(); this.session.begintransaction(); return this.session; }
that transactions
has been made for.
for instance:
try{ session = hibernateutil.getsessionfactory().opensession();//<==creade session sessionfactory tx = session.begintransaction();//<==== start transacton tx.settimeout(5); //dosomething(session); if 1 of queries fail insert goes catch tx.commit(); }catch(runtimeexception e){ try{ tx.rollback();//<=== you'll rolling previous inserting value in case if 1 failed insert }catch(runtimeexception rbe){ log.error("couldn’t roll transaction", rbe); } throw e; }finally{ if(session!=null){ session.close(); }
read more here.
Comments
Post a Comment