简体   繁体   English

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

[英]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. 我正在使用SpringBoot JPA Hibernate中的entity.merge将某些数据持久存储在Oracle数据库中,其中主键是Sequence生成的。 But after merge , I am not able to get the primary key which is inserted into the Oracle table. 但是合并之后,我无法获得插入到Oracle表中的主键。 For both the cases it is returning 0 对于这两种情况,它都返回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;

} }


And my Entity Class looks like : 我的实体类如下所示:

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

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 a persistent object, it doesn't turn the object you've sent it into a persistent one. merge方法返回一个持久对象,它不会将您发送给它的对象变成一个持久对象。

Here's an old doc but I believe the specific detail we discuss here remains unchanged in recent versions: https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#merge(java.lang.Object) 这是旧文档,但我认为我们在此处讨论的具体细节在最新版本中保持不变: https : //docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#merge(java.lang 。宾语)

So you just should do something like: 因此,您应该执行以下操作:

webCrtPart = session.merge(webCrtPart);

in your mergeEntity(webCrtPart) method 在您的mergeEntity(webCrtPart)方法中

The merge() method returns the actual entity that is the part of persistence context but not the passed object to the merge() method. merge()方法将实际实体(属于持久性上下文的一部分)返回给merge()方法,而不是传递的对象。 You can get the primary key from the returned Entity object by the merge() method. 您可以通过merge()方法从返回的Entity对象中获取主键。

WebCrtSerializedComponent webCrtCom = entityManager.merge(webCrtPart);

'webCrtCom' object will have the primary key. “ webCrtCom”对象将具有主键。 But aware of the persistence context of the webCrtCom object. 但是要知道webCrtCom对象的持久性上下文。

based on the structure of the example, it's likely that webCrtPartList is NOT the managed entity. 根据示例的结构,webCrtPartList可能不是受管实体。 You need to use the response of EntityManager.merge() to get the managed entity (the parameter to EntityManager.merge() will not [necessarily] be managed) 您需要使用EntityManager.merge()的响应来获取被管实体(EntityManager.merge()的参数将不会[被管理])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取任何JPA实体的主键? - How to get the primary key of any JPA entity? 提交事务后如何从实体对象获取ID(主键)? - How to get the ID (PRIMARY KEY) from an entity object after commit transaction? 我如何映射这个复合主键? - How do I map this composite primary key? 如何使一个实体作为另一个实体的主键 - How to make an entity as a primary key for the other entity 合并命令后获取主键值? - Getting the primary key value after a merge command? 当第一个实体是具有指定主键的实体之后的后续实体时,如何使用 JPA 获取列表 - How to getList using JPA when the first entity is the following entity after the entity with specified primary key 删除特定行后如何修改数据库 phpmyadmin 中的主键? - How do I modify the primary key in database phpmyadmin after deleting a specific row? 在休眠状态下合并具有复合主键的实体时发生异常 - Exception while merge a entity with composite primary key in hibernate 如何在不获取完整实体的情况下获取从属Hibernate实体的外键列值? - How do I get the foreign key column value of a dependent Hibernate Entity without fetching the full entity? 在驼峰中保存记录后如何获取生成的主键 - how to get generated primary key after persisting record in Camel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM