Annotation Interface PrePersist


@Target(METHOD) @Retention(RUNTIME) public @interface PrePersist
Declares a callback method for the corresponding lifecycle event. This annotation may be applied to methods of an entity class, a mapped superclass, or a callback listener class.

The @PrePersist callback for an entity being made persistent occurs before the entity is marked as managed and associated with the persistence context. When the merge() operation results in the creation of a new managed instance, the @PrePersist callback for the managed instance occurs after the merged entity state has been copied to it. The PrePersist method is always invoked synchronously during execution of the persist() or merge() operation.

A generated primary key value is available when this callback occurs only for UUID, TABLE, or SEQUENCE primary key generation. For IDENTITY primary key generation, the generated primary key is not available when this callback occurs.

The following rules apply to lifecycle callback methods:

  • Lifecycle callback methods may throw unchecked/runtime exceptions. A runtime exception thrown by a callback method that executes within a transaction causes that transaction to be marked for rollback if context is joined to the transaction.
  • Lifecycle callbacks can invoke JNDI, JDBC, JMS, and enterprise beans.
  • A lifecycle callback method may modify the non-relationship state of the entity on which it is invoked.
  • In general, the lifecycle method of a portable application should not invoke EntityManager or query operations, access other entity instances, or modify relationships within the same persistence context

It is implementation-dependent whether callback methods are invoked before or after the cascading of the lifecycle events to related entities.

Since:
1.0