开发者

merge is used only for creating or updating?

开发者 https://www.devze.com 2023-01-18 11:14 出处:网络
If i use ex.merge(obj), now if in object obj i set the primary key to a value which is not present in database, will it create a new record or will it throw an exception?

If i use ex.merge(obj), now if in object obj i set the primary key to a value which is not present in database, will it create a new record or will it throw an exception?

for example

if obj with pk val = 19 doesnot exist in database,and i set
obj.setPk(20);
obj.setName("nm");

em.merge(obj)  // will this throw an exception or 开发者_运维百科create a new record?`enter code here`


It will create a new record:

  • if the entity is already in the persistence context (session), no action is taken, except for cascades

  • if the entity is detached, a copy (object') is returned, which is attached (managed)

  • if the entity is transient (new instance), it is saved and a persistent (and managed) copy is returned

0

精彩评论

暂无评论...
验证码 换一张
取 消