Do not confuse flushing with fixation. During flush()
JPA provider physically sends the generated SQL to the database and, in your case, reads the generated identifier and populates it into the bean. Note that you should always use the return object, not the original one that was passed to persist()
:
token = em.persist(token);
Strike>
On the other hand, committing a transaction with a database commit. Obviously, it will call flush()
, but that will not help you. But since you ask - every method in EJB is transactional by default. This means that the transaction is committed when you leave the first EJB on the stack: if you call one EJB from another, the caller joins the callerβs transaction by default (see Transaction Behavior).
Also note that the rules when flush()
bit complicated, as each provider tries to do this as late as possible and in packages.
source share