Sunday, 13 September 2015

Many to Many Implementation in Liferay

Liferay providing mapping-table attribute for column from this we can achieve many to many relation between entities (tables)

The entities which are participated in many to many relations, for those entities(tables) we have to define column and that column should have mapping-table attribute and entity attributes.

The column which contains mapping-table those columns are really/physically not existed in the database tables, the column are virtual columns for entity which will support many to many relation between table

When we use Many to Many Relation between tables we will use mapping–table attribute for the column. Whatever the value we provided to mapping-table attribute then service builder create another new tables with that value.

The created table will have two columns and those columns are representing each table primary keys i.e. the tables participated in May to Many Relation.

Example:

Assume we have employee and Circle tables. When we implement Many to Many relation between these tables we will get new table called Employee_Circle and the table contains two columns and the columns are primary keys of employee and Circle

This Employee_Circle is the value of mapping-table attribute in entity column.

Service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.test.training">
<author>Veera Reddy</author>
<namespace>mmmp</namespace>
<entity name="Employee" local-service="true" remote-service="true" cache-enabled="false">
<column name="employeeId" type="long" primary="true" />
<column name="employeeName" type="String" />
<column name="employeePlace" type="String" />
<column name="Circles" type="Collection" entity="Circle" mapping-table="Employee_Circle"/>
</entity>
<entity name="Circle" local-service="true" remote-service="true" cache-enabled="false">
<column name="circleId" type="long" primary="true" />
<column name="circleName" type="String" />
<column name="circleDescription" type="String" />
<column name="Employees" type="Collection" entity="Employee" mapping-table="Employee_Circle" />
</entity>
</service-builder>
 Run the service builder you will get generated following sql tables.
create table mmmp_Circle (
circleId LONG not null primary key,
circleName VARCHAR(75) null,
circleDescription VARCHAR(75) null
);

create table mmmp_Employee (
employeeId LONG not null primary key,
employeeName VARCHAR(75) null,
employeePlace VARCHAR(75) null
);

create table mmmp_Employee_Circle (
employeeId LONG not null,
circleId LONG not null,
primary key (employeeId, circleId)
);
Observation:
The columns which contains mapping-table attribute those column really not presented in table’s SQL as columns.
In service.xml we have defined only 2 tables but service builder created another tables and the table name i.e. we have provided value for mapping-table attribute in the column.
The mapping table has two columns these columns represents primary keys of other tables which are participated in many to many relation.

No comments:

Post a Comment