开发者

How to make Play create a new ID(auto increment) when doing a merge with existing object

Right now I'm doing this but it seems to not work right. Thanks for any help.

   public static void submit(@Valid WrapSpec wrapSpec) { 
        if (validation.hasErrors()) { 
            render("@index", wrapSpec); 
        } 
 JPA.em().detach(wrapSpec); 
     开发者_C百科       wrapSpec.wrapSpecId = null; 
            WrapSpec wrapSpecNew = wrapSpec.merge(); 
} 


If you are using Play's Model class in you object, they have an automatic autoincrement field named "id".

The only safe way to reassign this id would be to create a new object and copy all the properties (expect id) from the old object to the new one, and then save it. JPA will assign the new id.

Any other way may create database inconsistencies or unexpected behavior in JPA/Play.

Out of curiosity, why would you like to change the id once assigned? I don't see how it may be useful...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜