java - Reusing Persistence Context in JPA -
i working on @embededid code, want auto increment before entity persisted, want without use of @generatedvalue , identity column,
below table composite id,
create table tbl_employee_002( id integer, country varchar(50), name varchar(50), constraint pk_emp_00240 primary key(id,country) )
this code entity mapping,
@entity @table(name="tbl_employee_002") public class employeeentitysix implements serializable{ // contructor's @embeddedid private employeeidtwo id; @column(name="name") private string employeename; // getters , setter's @prepersist public void incid(){ entitymanager em = null; query q = null; entitymanagerfactory emf = null; try{ emf = persistence.createentitymanagerfactory("forpractise"); em = emf.createentitymanager(); q = em.createquery("select max(e.id.employeeid) employeeentitysix e"); list list = q.getresultlist(); integer = (list != null && list.size() > 0) ? integer.valueof(list.get(0).tostring()) : 0; this.getid().setemployeeid(++i); }catch(exception e){ system.out.println("excetion while increasing counter..."); e.printstacktrace(); }finally{ if(em != null && em.isopen()){ em.close(); } if(getemf() != null && getemf().isopen()){ getemf().close(); } } }
this composite id mapping,
@embeddable public class employeeidtwo implements serializable{ @column(name="id") private integer employeeid; @column(name="country",length=50) private string empcountry; // getters , setters }
this code of main method, main method in other class,
public static void main(string [] args){ entitymanagerfactory emf = null; entitymanager em = null; entitytransaction tx = null; try{ emf = persistence.createentitymanagerfactory("forpractise"); em = emf.createentitymanager(); tx = em.gettransaction(); tx.begin(); employeeentitysix employee = new employeeentitysix(new employeeidtwo("zimbabwe"), "henry olanga"); em.persist(employee); .... }
now above code runs fine, whenever persist entity "employeeentitysix", method annotated @perpersist runs, first fetch max id, increments its, set id in embeded entity , persist entity.
now question is,
i creating entitymanagerfactory twice, first in main method, second time in @prepersist method in entity employeeentitysix. whether can use first entitymanagerfactory created in main method in entity employeeentitysix while pre-persist, or else whether can reuse entitymanager created in first time in main method in @prepersist method in entity.
just information, using plain java environment, not using java ee container.
hibernate default tries persist fields of entity class or embedded id, including field emf
, not know how persist field of type entitymanagerfactory
.
of course not make sense persist entitymanagerfactory
. mark field @transient
prevent being persisted, going face different problems.
the injection of entitymanagerfactory
@persistenceunit
annotation works on cdi beans , ejbs in applications run on java ee-compliant application server. using main method, assume example simple jse program.
furthermore should not access entitymanager
s in lifecycle callback methods such @prepersist
. quote jpa specification (jsr 317: javatm persistence api, version 2.0):
in general, lifecycle method of portable application should not invoke entitymanager or query operations, access other entity instances, or modify relationships within same persistence context. lifecycle callback method may modify non-relationship state of entity on invoked.
i suggest keep entitymanagerfactory
out of embedded id class , rid of incid
-method. instead execute query determine new employeeid
in main method, before calling persist
. works fine long 1 instance of program works database. when there multiple programs trying insert new employees there race conditions 2 programs try insert same id.
in order prevent can use database sequence generate employeeid
, annotations @generatedvalue
, @sequencegenerator
. find more information id generation here: http://en.wikibooks.org/wiki/java_persistence/identity_and_sequencing#sequencing
Comments
Post a Comment