Annotation Interface OnCreate


Annotates Permazen model class methods that are to be invoked whenever a database object is newly created.

Note that there is a subtle distinction between (a) the creation of database objects in the database, and (b) the instantiation of Java model objects that represent database objects (i.e., PermazenObjects). These two events do not occur at the same time; in particular, distinct Java model objects are instantiated to represent the same database object in different transactions. In addition, it's even possible for a Java model object to be instantiated when no corresponding database object exists in the database, e.g., via PermazenTransaction.get(io.permazen.core.ObjId).

Methods that are annotated with @OnCreate are invoked only for events of type (a). As a consequence, for any database fields that require default initialization, this initialization should be performed not in a Java constructor but rather in an @OnCreate-annotated method.

For example, instead of this:

  @PermazenType
  public abstract class Event {

      protected Event() {
          this.setCreateTime(new Date());
      }

      @NotNull
      public abstract Date getCreateTime();
      public abstract void setCreateTime(Date createTime);

      ...
 

do this:

  @PermazenType
  public abstract class Event {

      @OnCreate
      private void initializeCreateTime() {
          this.setCreateTime(new Date());
      }

      @NotNull
      public abstract Date getCreateTime();
      public abstract void setCreateTime(Date createTime);

      ...
 

Notifications are delivered in the same thread that created the object, immediately after the object is created.

The annotated method must be an instance method (i.e., not static), return void, and take zero parameters. It may have any level of access, including private.

Some notifications may need to be ignored by objects in detached transactions; you can use this.isDetached() to detect that situation.

Actions that have effects visible to the outside world should be made contingent on successful transaction commit, for example, via Transaction.addCallback().

Meta-Annotations

This annotation may be configured indirectly as a Spring meta-annotation when spring-core is on the classpath.