Instead of having to create staged models for your app manually, you can leverage Service Builder to generate the necessary staged model logic for you. If your app doesn’t use Liferay’s Service Builder, you must configure it in your project. This
tutorial assumes you have a Service Builder project with *api
and *service
modules. If you want to follow along with this tutorial, download the
staged-model-example
Service Builder project (Gradle-based).
You’ll track the Service Builder-generated changes applied to an entity model
file to observe how staged models are assigned to your entity. Keep in mind the
specific
staged attributes
necessary for each staged model. Depending on the attributes defined in your
service.xml
file, Service Builder assigns your entity model to a specific
staged model type.
-
Navigate to your project’s
*service
module at the command line. Run Service Builder (e.g.,gradlew buildService
) to generate your project’s models based on the currentservice.xml
configuration. -
Open your project’s
[Entity]Model.java
interface and observe the inherited interfaces.public interface FooModel extends BaseModel<Foo>, ShardedModel, StagedModel {
Your model was generated as a staged model! This is because the
service.xml
file sets the UUID totrue
and defines thecompanyId
,createDate
, andmodifiedDate
columns. There is much more logic generated for your app behind the scenes, but this shows that Service Builder deemed your entity eligible for the Staging and Export/Import frameworks. -
Add the
userId
anduserName
columns to yourservice.xml
file:<column name="userId" type="long" /> <column name="userName" type="String" />
-
Rerun Service Builder and observe your
[Entity]Model.java
interface again:public interface FooModel extends BaseModel<Foo>, GroupedModel, ShardedModel, StagedAuditedModel {
Your model is now a staged audited model!
-
Add the
lastPublishDate
column to yourservice.xml
file:<column name="lastPublishDate" type="Date" />
-
Rerun Service Builder and observe your
[Entity]Model.java
interface again:public interface FooModel extends BaseModel<Foo>, ShardedModel, StagedGroupedModel {
Your model is now a staged grouped model! The
groupId
column is also required to extend theStagedGroupedModel
interface, but it was already defined in the originalservice.xml
file.
Fantastic! You’ve witnessed firsthand how easy it is to generate staged models using Service Builder.