Defining Entity Columns

An entity’s columns represent its attributes. These attributes map table fields to Java object fields.

Here you’ll examine the sample project from Understanding and Extending Generated Classes to learn more details about columns.

Start by downloading and unzipping the example:

curl https://resources.learn.liferay.com/dxp/latest/en/building-applications/data-frameworks/service-builder/service-builder-basics/liferay-w9b7.zip -O
unzip liferay-w9b7.zip

Examine the w9b7-service/service.xml file:

<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.4.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_7_4_0.dtd">

<service-builder dependency-injector="ds" package-path="com.acme.w9b7" short-no-such-exception-enabled="false">
	<namespace>W9B7</namespace>
	<entity local-service="true" name="W9B7Entry" remote-service="false">

		<!-- PK fields -->

		<column name="w9b7EntryId" primary="true" type="long" />

		<!-- Other fields -->

		<column name="description" type="String" />
		<column name="name" type="String" />
	</entity>
</service-builder>

To add attributes for your entity, add <column /> tags to your entity definition:

<column name="w9b7EntryId" primary="true" type="long" />

When Service Builder runs, it generates a Hibernate configuration that handles the object-relational mapping. Service Builder creates a database field for each column you add to the service.xml file. It maps a database field type appropriate to the Java type specified for each column, and it does this across all the databases Liferay supports. Service Builder also generates getter/setter methods in the model class for these attributes. If a column’s primary (i.e., primary key) attribute is set to true, the column becomes the primary key for the entity. This is the case in the W9B7 application found in Understanding and Extending Generated Classes. If you define multiple columns with the primary attribute set to true, the combination of columns makes up a compound primary key for the entity. See Implementing an Add Method to learn how to generate unique primary keys for entity instances.

Create Entity Columns

Define the columns you need for your first entity. The W9B7 entity has only three attributes: a primary key, description, and name.

<column name="w9b7EntryId" primary="true" type="long" />
<column name="description" type="String" />
<column name="name" type="String" />
Note

On deploying a *service module, Service Builder automatically generates indexes for all entity primary keys.

Create a column for each attribute of your entity or entities, using the Java type you’ll use in your application. Service Builder handles mapping it to SQL for you.

Support Multi-tenancy

In addition to columns for your entity’s primary key and attributes, add portal instance ID and site ID columns. Then you can support Liferay’s multi-tenancy features, so that each portal instance and each Site in a portal instance can have independent sets of your application’s data. To hold the site’s ID, add a column called groupId of type long. To hold the portal instance’s ID, add a column called companyId of type long:

<!-- Group instance -->

<column name="groupId" type="long" />
<column name="companyId" type="long" />

Workflow Fields

You can support Liferay’s workflow system by adding the fields it needs to track an entity’s progress:

<!-- Status fields -->

<column name="status" type="int" />
<column name="statusByUserId" type="long" />
<column name="statusByUserName" type="String" />
<column name="statusDate" type="Date" />

Audit Entities

Finally, you can add columns to help audit your entities. To track each entity instance’s owner, add a column called userId of type long. Create a column named createDate of type Date to note an entity instance’s creation date. Add a column named modifiedDate of type Date to track the last time an entity instance was modified.

<!-- Audit fields -->

<column name="userId" type="long" />
<column name="userName" type="String" />
<column name="createDate" type="Date" />
<column name="modifiedDate" type="Date" />

Great! Your entities have columns that not only represent their attributes, but also support multi-tenancy, workflow, and auditing.

Capabilities

Product

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy