java - JPA entities updated implicitly by new query -
java - JPA entities updated implicitly by new query -
the title can't reflect question, don't know how express. have jpa entity (vatoperatorbalance
has field salebalance
), lets retrieve entity @ first time, , entity (vatoperatorbalance@3d6396f5
), salebalance
100.0
. there other operations has modified salebalance
200
, query database , new entity (vatoperatorbalance@10f8ed
), sure salebalance
of entity 200.0
. create me confused salebalance
of old entity (vatoperatorbalance@3d6396f5
) 200.0
.
all these queries , operations in single transaction, , query isn't entitymanager.find(java.lang.class<t> entityclass, java.lang.object primarykey)
homecoming entity cache.
below code
@rollback(true) @test public void testsale_singlebet_ok() throws exception { // prepare request ... // query vatoperatorbalance first vatoperatorbalance oldbalance = this.getvatoperatorbalancedao().findbyoperator("operator-111"); //this.entitymanager.detach(oldbalance); logger.debug("------ oldbalance(" + oldbalance + ")."); // operation modify oldbalance context salereqctx = this.getdefaultcontext(transactiontype.sell_ticket.getrequesttype(), clientticket); salereqctx.setgametypeid(gametype.vat.gettype() + ""); context salerespctx = dopost(this.mockrequest(salereqctx)); raffleticket respticket = (raffleticket) salerespctx.getmodel(); this.entitymanager.flush(); this.entitymanager.clear(); // assert vat sale balance vatoperatorbalance newbalance = this.getvatoperatorbalancedao().findbyoperator("operator-111"); logger.debug("------ newbalance(" + newbalance + ")."); assertequals(oldbalance.getsalebalance().add(respticket.gettotalamount()).doublevalue(), newbalance .getsalebalance().doublevalue(), 0); }
this testcase fail, don't understand why happen. jpa entity manager update entities of same entity type? oldbalance
entity , newblance
entity have same entityid
, different java instance, happened in jpa entity manager? if detach oldbalance
entity entitymanager
, testcase pass.
note: test using spring4.0.5 , jpa2.1
@piet.t since entitymanager recognize same entity primary key (feel free seek it). changes made entity through same entitymanager impact same java instance
so in entity manager, given entity type given primary key, there should 1 java instance or managed entity(if query entity manager, no matter query criteria, id or not, same java instance(managed entity) returned).
however in test case, entity 'oldbalance' updated "the operation modify oldbalance", , phone call of entitymanager.clear() detach entities managed entity manager, says 'oldbalance' detached too.
and 'newbalance' managed entity then, why have different java instance identifier. if 'oldbalance' managed, illustration phone call entitymanager.merge(), same instance of 'newbalance'.
i think of confusion arise flush()
-call in code.
flush
store changed value database - that's whoe point of calling flush
. when using transactions changed value might still not visible via other connections due databases transaction machanism entitymanager see changed value. without clear
-call query - though not using find
- still homecoming same instance created (vatoperatorbalance@3d6396f5) since entitymanager recognize same entity primary key (feel free seek it). changes made entity through same entitymanager impact same java instance while modifications through entity manager cause exception because entity update transaction. some queries might cause implicit flush
, since cached changes might influence query-result, changes have written database before executing query right result-set. i hope help bit.
java jpa
Comments
Post a Comment