Today we will discuss Many To Many relationship in Liferay
As discuss earlier Liferay Service Builder create tables with service.xml. But is there any way to provide Many To Many relationship between entities. Yes there is today we will discuss Many To Many relationship in liferay. Before Reading this blog it is highly recommended to read my previous blog on Service Builder in Detail.
Lets Start this step by step:-
Step 1:-Create service.xml
You can create service as mention in my previous article Service Builder in Detail .
service.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd"> | |
<service-builder package-path="com.aditya" auto-namespace-tables="false"> | |
<author>aditya.bhardwaj</author> | |
<namespace>emplo</namespace> | |
<entity name="Employee" local-service="true" remote-service="false" table="Employee"> | |
<column name="eid" type="long" primary="true"></column> | |
<column name="name" type="String"></column> | |
<column name="address" type="String"></column> | |
<column name="deptid" type="Collection" entity="Department" mapping-table="Employee_department"/> | |
</entity> | |
<entity name="Department" local-service="true" remote-service="false" table="Department"> | |
<column name="deptid" type="long" primary="true"></column> | |
<column name="department" type="String"></column> | |
<column name="eid" type="Collection" entity="Employee" mapping-table="Employee_department"/> | |
</entity> | |
</service-builder> |
Just focus on line no 11 and 17:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<column name="deptid" type="Collection" entity="Department" mapping-table="Employee_department"/> | |
<column name="eid" type="Collection" entity="Employee" mapping-table="Employee_department"/> |
Explanation:-
Here we create a third entity(table) Employee_department Which contain the primary key of both tables.
Step 2:-Check point
Now build the service.xml and open tables.sql inside sql folder.
tables.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table Department ( | |
deptid LONG not null primary key, | |
department VARCHAR(75) null | |
); | |
create table Employee ( | |
eid LONG not null primary key, | |
name VARCHAR(75) null, | |
address VARCHAR(75) null | |
); | |
create table Employee_department ( | |
deptid LONG not null, | |
eid LONG not null, | |
primary key (deptid, eid) | |
); |
As you can see 3 tables are created two for entities and one for mapping.
Step 3:-Calling Services
Just create a doView() method and add data to the tables here we consider a scenario where one employee belong to two department.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { | |
try { | |
Employee employee; | |
employee = EmployeeLocalServiceUtil.createEmployee(CounterLocalServiceUtil.increment()); | |
employee.setAddress("Delhi"); | |
employee.setName("Ramesh"); | |
employee = EmployeeLocalServiceUtil.addEmployee(employee); | |
Department department1,department2; | |
department1 = DepartmentLocalServiceUtil.createDepartment(CounterLocalServiceUtil.increment()); | |
department1.setDepartment("IT"); | |
department1 =DepartmentLocalServiceUtil.addDepartment(department1); | |
department2 = DepartmentLocalServiceUtil.createDepartment(CounterLocalServiceUtil.increment()); | |
department2.setDepartment("Research"); | |
department2 =DepartmentLocalServiceUtil.addDepartment(department2); | |
DepartmentLocalServiceUtil.addEmployeeDepartments(employee.getEid(), new long[]{department1.getDeptid(),department2.getDeptid()}); | |
} | |
catch (SystemException e) { | |
e.printStackTrace(); | |
} | |
} |
Thats it just see the tables in database:-
You can Download source code from Many To Many mapping in Liferay
Hope this will help....
Related Post:-
Liferay Service Builder in Detail
Finder Method for Service Builder in Liferay
Custom Sql in Liferay
Custom Sql with two table in Liferay
Liferay Service Builder in Detail
How to install Maven
Creating Portlet/Services using Maven
No comments:
Post a Comment