Objective:
Implement Many to Many Relation in Liferay development using service builder tool.
In our real time development we come across Many to Many relation mapping when we interact with data base.
Hibernate Object relational mapping support one to many and many to many relations. Coming to liferay we never directly implement hibernate in liferay development and we just use service builder tool so that we can generate all data base services with minimal effort.
Liferay service builder support many to many relation in liferay.
Scenario
A student can have many courses and course can be taken by many students
Introduction:
Liferay have very good tool called service builder from which we can build required services for portlet. Services are nothing but database interaction related java classes, interfaces and its configuration files. Service builder uses spring and hibernate to build service layer for portlet.
To build service layer for portlet service builder need one configuration file called service.xmlby using this it will get information to generate required service classes and interfaces.
We already aware of spring and hibernate integration in normal web applications. Same way service builder build model/POJO classes, DAO classes and service classes.
Along with java classes some spring configuration files and Hibernate mapping files will be generated.
Spring will be load all required services in application context so that we can use required methods from some Util classes
The following are the classes and interface for each entity which we defined in service.xml
XXXModel XXXModelmpl XXXPersistence XXXPersistenceImpl XXXServiceImpl XXXLocalServiceImpl XXXServiceUtil XXXLocalServiceUtil XXXUtil Here XXX is Entity Name Note: In service builder each table will be referred as Entity In Development we will use only Util classes to do database interaction |
Many to Many Implementation in Liferay
Service builder can support Many to Many Relation so that we can use this functionality in real time development
In the service builder we will define entities and its columns in service.xmlfile from this information Service Buildergenerated the required services to each entity. These services will be interacted with data base tables.
Liferay providing mapping-table attribute for column from this we can achieve many to many relation between tables
What are the tables or entities which are participated in many to many relations, for those tables we have to define column and that column should have mapping-tableattribute and entity attributes.
What are 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-tableattribute 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.
Concept Example:
Assume we have Studentand Course tables. When we implement Many to Many relation between these tables we will get new table called Students_Courses and the table contains two columns and the columns are primary keys of Student and Course
This Student_Coursesis the value of mapping-table attribute in entity column.
Example of service.xml
<service-builder package-path="com.meera.db"> <author>E5410</author> <namespace>meera</namespace> <entity name="Student" local-service="true" remote-service="true" cache-enabled="false"> <column name="studentId" type="long" primary="true" /> <column name="studentName" type="String" /> <column name="studentPlace" type="String" /> <column name="studentCollege" type="String" /> <column name="courses" type="Collection" entity="Course" mapping-table="Students_Courses"/> </entity> <entity name="Course" local-service="true" remote-service="true" cache-enabled="false"> <column name="courseId" type="long" primary="true" /> <column name="courseName" type="String" /> <column name="courseGroup" type="String" /> <column name="students" type="Collection" entity="Student" mapping-table="Students_Courses" /> </entity> </service-builder> |
The following table will be created in data base
create table meera_Course ( courseId LONG not null primary key, courseName VARCHAR(75) null, courseGroup VARCHAR(75) null ); create table meera_Student ( studentId LONG not null primary key, studentName VARCHAR(75) null, studentPlace VARCHAR(75) null, studentCollege VARCHAR(75) null ); create table meera_Students_Courses ( courseId LONG not null, studentId LONG not null, primary key (courseId, studentId) ); |
Note:
Liferay service builder created tables and the table names appended with name space.
Observation:
- The columns which contains mapping-table attribute those column really not presented in table’s SQL as columns.
- In service.xmlwe have defined only 2 tables but service builder created another tables and the table name i.e. we have provided value for mapping-tableattribute 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.
0 comments:
Post a Comment