I have a simple JPA entity that uses a generated long
"ID" as its primary key:
@Entity
public class Player {
private long id;
protected Player() {
// Do nothing; id defaults to 0L
}
@GeneratedValue
@Id
public long getId() {
return id;
}
protected void setId(final long id) {
this.id = id;
}
// Other code
}
At some point in the life-cycle of an object of this type the JPA must call setId()
to record the generated ID value. My question is, when does this happen, and where is the documentation that states this. I've looked through the JPA Specification and can not find a clear statement.
The JPA Specification says (emphasis added):
A managed entity instance is an instance with a persistent identity that is currently associated with a persistence context.
Is that trying to say that the the object must be managed to have its @Id
significant?
The documentation for EntityManager.persist()
says (emphasis added) it makes "an instance managed and persistent", so does that mean that the @Id
is set by that method? Or is it not until you call EntityTransaction.commit()
?
When the @Id
is set might be different for different JPA providers, and perhaps for different generation strategies. But what is the safest (portable, specification conforming) assumption that you can make about the earliest point in the lifecycle that it has been set?