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...
精彩评论