sromit sromit - 27 days ago 6x
Java Question

How do I get the primary Key after entity.merge

I am using entity.merge from my SpringBoot JPA Hibernate to persist certain data in Oracle database where primary key is Sequence generated.
But after merge , I am not able to get the primary key which is inserted into the Oracle table. For both the cases it is returning 0"Inside InsertParts>>>>Before Merge Component-serialized_id>>>>" +webCrtPart.getSerializedComponentId()+ ">>>CSI>>" +webCrtPart.getCsInvSerialNumber());
// Persist entity to database

// checking the batchsize so after every batch of batchsize , it
// gets inserted to database
if (i % batchSize == 0) {
// Flush a batch of inserts and release memory.
}"Inside InsertParts>>>>After merge Component-serialized_id>>>>"+webCrtPart.getSerializedComponentId());

return webCrtPartList;


And my Entity Class looks like :

@Table(name = "WEBCRT_CMPNT_SRLZD")
public class WebCrtSerializedComponent {

@SequenceGenerator(name="component_seq", sequenceName="WEBCRT_CMPNT_SRLZD_SEQ" , allocationSize=1)
@GeneratedValue(generator = "component_seq", strategy = GenerationType.SEQUENCE)
@Column(name = "CMPNT_SRLZD_SEQ_ID")
private Long serializedComponentId;

@Column(name = "ITM")
private String item;

@Column(name = "SUB_CMPNT_SEQ_ID")
private String subComponentSeqId;

@Column(name = "PRT_NUM")
private String partNumber;

@Column(name = "INCMNG_SRL_NUM")
private String incomingSerialNumber;

@Column(name = "NUM_OF_PREV_RPRS")
private String previousRepairCount;

Please find the following log:

2016-09-28 15:42:19.861[0;39m [32m INFO[0;39m [35m63710[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization completed in 25 ms
[2m2016-09-28 15:42:19.934[0;39m [32m INFO[0;39m [35m63710[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mc.g.p.b.m.impl.JobRetrievalManager [0;39m [2m:[0;39m parameters to be passed : - 159693-----10
[2m2016-09-28 15:42:20.484[0;39m [32m INFO[0;39m [35m63710[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mc.g.p.b.managers.impl.PartSynchManager [0;39m [2m:[0;39m JobControllerV1 >>>>>>>1
[2m2016-09-28 15:42:20.491[0;39m [32m INFO[0;39m [35m63710[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mc.g.p.b.r.PartSynchRepositoryImpl [0;39m [2m:[0;39m Inside InsertParts>>>>Before Merge Component-serialized_id>>>>0>>>CSI>>null
Hibernate: select webcrtseri0_.cmpnt_srlzd_seq_id as cmpnt_srlzd_seq_id1_3_0_, webcrtseri0_.child_sys_asmbly_id as child_sys_asmbly_i2_3_0_, webcrtseri0_.crtd_by as crtd_by3_3_0_, webcrtseri0_.crtn_dt as crtn_dt4_3_0_, webcrtseri0_.csinv_sl_num as csinv_sl_num5_3_0_, webcrtseri0_.cust_supplied_ind as cust_supplied_ind6_3_0_, webcrtseri0_.incmng_dpm_ind as incmng_dpm_ind7_3_0_, webcrtseri0_.incmng_srl_num as incmng_srl_num8_3_0_, webcrtseri0_.itm as itm9_3_0_, webcrtseri0_.job_seq_id as job_seq_id10_3_0_, webcrtseri0_.lst_updtd_by as lst_updtd_by11_3_0_, webcrtseri0_.lst_updt_dt as lst_updt_dt12_3_0_, webcrtseri0_.outgng_dpm_ind as outgng_dpm_ind13_3_0_, webcrtseri0_.otgng_pn as otgng_pn14_3_0_, webcrtseri0_.otgng_sn as otgng_sn15_3_0_, webcrtseri0_.prt_num as prt_num16_3_0_, webcrtseri0_.prev_job_num as prev_job_num17_3_0_, webcrtseri0_.num_of_prev_rprs as num_of_prev_rprs18_3_0_, webcrtseri0_.repl_ind as repl_ind19_3_0_, webcrtseri0_.sub_cmpnt_seq_id as sub_cmpnt_seq_id20_3_0_, webcrtseri0_.set_sys_asmbly_id as set_sys_asmbly_id21_3_0_ from webcrt_cmpnt_srlzd webcrtseri0_ where webcrtseri0_.cmpnt_srlzd_seq_id=?
Hibernate: select WEBCRT_CMPNT_SRLZD_SEQ.nextval from dual
[2m2016-09-28 15:42:20.628[0;39m [32m INFO[0;39m [35m63710[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mc.g.p.b.r.PartSynchRepositoryImpl [0;39m [2m:[0;39m Inside InsertParts>>>>After merge Component-serialized_id>>>>0
Hibernate: insert into webcrt_cmpnt_srlzd (child_sys_asmbly_id, crtd_by, crtn_dt, csinv_sl_num, cust_supplied_ind, incmng_dpm_ind, incmng_srl_num, itm, job_seq_id, lst_updtd_by, lst_updt_dt, outgng_dpm_ind, otgng_pn, otgng_sn, prt_num, prev_job_num, num_of_prev_rprs, repl_ind, sub_cmpnt_seq_id, set_sys_asmbly_id, cmpnt_srlzd_seq_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)


The merge() method returns the actual entity that is the part of persistence context but not the passed object to the merge() method. You can get the primary key from the returned Entity object by the merge() method.

WebCrtSerializedComponent webCrtCom = entityManager.merge(webCrtPart);

'webCrtCom' object will have the primary key. But aware of the persistence context of the webCrtCom object.