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.xmlYou can create service as mention in my previous article Service Builder in Detail .
service.xml | <?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:- | <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 pointNow build the service.xml and open tables.sql inside sql folder.
tables.sql | 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 ServicesJust create a doView() method and add data to the tables here we consider a scenario where one employee belong to two department.
| 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
TEST
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.
0 comments:
Post a Comment