hibernate - EntityManager.flush(), EntityManager.getTransaction.commit(), What is the standard usage ? -
i'm trying understand how synchronisation between persistence context , database works.the code below runs pretty :
entitymanagerfactory factory = persistence.createentitymanagerfactory("projecta"); entitymanager em = factory.createentitymanager(); em.gettransaction().begin(); for(string name : names) { student toinsert = new student(); toinsert.setname(name); em.persist(toinsert); } em.gettransaction().commit();
but
flush()
method goes ? have use ?do have
gettransaction.begin()
everytime ?what standard form update information in database hibernate ?
- flush not required commit flush automatically you. need flush when performing long running transaction data needs saved db avoid memory heap (or) if need sync data in session db reason.
- if feel starting transactions tedious, ma use spring declarative transactions. doing so, spring take care of creating transaction boundaries around method , commits/rolls data depending on happens in method. easy use, has own limitations not allow handle rollback.
- updating object depends on context of object(has 1 object in session/no object in session) use session.merge(object) solves couple of scenarios you. make sure, have data set on detached object needs saved, or else end deleting data not loaded.
Comments
Post a Comment