How to set id in model? I try this code:
public Community(Long id, ...) {
this.id = id;
....
}
But when I do this:
Communtiy c = new (1, ...);
c.save();
Hibernate say:
Execution exception PersistenceException occured : org.hibernate.PersistentObjectException: detached entity passed to persist: models.Community
You can do this by extending from
play.db.jpa.GenericModelinstead ofplay.db.jpa.Model. This will allow you to set the primary key manually.In fact, what
Modeldoes is extend fromGenericModeland because it is very common to do soModelautomatically generates primary keys ( by using@Id @GeneratedValue public Long id). But if for whatever reason you want to set a custom primary key, then extending fromGenericModelis the way to go.Official documentation here.
Example