[#lifecycle-events]
= Lifecycle Events


:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:


Lifecycle events allow domain object subscribers to listen for changes to the persistence state of domain entities, and act accordingly.

[WARNING]
====
Lifecycle events are _not_ fired for view models.
====

The lifecycle events supported are:

* *created*
+
Entity has just been instantiated.
Note that this requires that the object is instantiated using the framework, see xref:userguide:ROOT:domain-services.adoc#instantiating[here] for further discussion.

* *loaded*
+
Entity has just retrieved/rehydrated from the database

* *persisting*
+
Entity is about to be inserted/saved (ie for the first time) into the database

* *persisted*
+
Entity has just been inserted/saved (ie for the first time) into the database

* *updating*
+
The (already persistent) entity about to be flushed in the database

* *updated*
+
The (already persistent) entity has just been flushed to the database

* *removing*
+
The (already persistent) entity is about to be deleted from the database

For example, if annotated with xref:refguide:applib:index/annotation/DomainObject.adoc#updatingLifecycleEvent[@DomainObjectLayout#updatingLifecycleEvent], the appropriate (subclass of) `ObjectUpdatingEvent` will be emitted.

There is no lifecycle event for "entity creating" because (obviously) the framework doesn't know about newly created objects until they have been created.
Similarly, there is no lifecycle event for entities that have been removed because it is not valid to "touch" a domain entity once deleted.

