如何在entity.merge之后获取主键

时间:2016-09-28 20:40:53

标签: java oracle hibernate jpa

我正在使用SpringBoot JPA Hibernate中的entity.merge来保存Oracle数据库中的某些数据,其中主键是Sequence生成的。 但是在合并之后,我无法获得插入Oracle表的主键。对于这两种情况,它都返回0

LOG.info("Inside InsertParts>>>>Before Merge Component-serialized_id>>>>" +webCrtPart.getSerializedComponentId()+ ">>>CSI>>" +webCrtPart.getCsInvSerialNumber());
    // Persist entity to database
    mergeEntity(webCrtPart);

    i++;
    // 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.
        entityManager.flush();
        entityManager.clear();
    }

    LOG.info("Inside InsertParts>>>>After merge Component-serialized_id>>>>"+webCrtPart.getSerializedComponentId());
}

return webCrtPartList;

}

我的实体类看起来像:

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

    @Id 
    @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;

请找到以下日志:

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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

3 个答案:

答案 0 :(得分:2)

merge方法返回一个持久对象,它不会将你发送给它的对象变成持久对象。

这是一个旧文档,但我相信我们在这里讨论的具体细节在最近的版本中保持不变: https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#merge(java.lang.Object)

所以你应该做的事情如下:

webCrtPart = session.merge(webCrtPart);
你的mergeEntity(webCrtPart)方法中的

答案 1 :(得分:1)

merge()方法返回作为持久化上下文一部分的实际实体,但不返回merge()方法的传递对象。 您可以通过merge()方法从返回的Entity对象中获取主键。

WebCrtSerializedComponent webCrtCom = entityManager.merge(webCrtPart);

' webCrtCom'对象将具有主键。但是要注意webCrtCom对象的持久化上下文。

答案 2 :(得分:0)

基于示例的结构,webCrtPartList可能不是托管实体。您需要使用EntityManager.merge()的响应来获取被管实体(EntityManager.merge()的参数不会[必然]被管理)