Showing posts with label liferay many to many. Show all posts
Showing posts with label liferay many to many. Show all posts

Sunday, January 12, 2014

Liferay Service Builder Many to Many Relation in Plugin Portlet Part-III

Objective:

Implement Many to Many Relation in liferay development using service builder tool.



Environment:

Liferay 6.2 +Tomcat 7.x+MySQL 5.1

Note:

The code will work for portal 6.2 version you can try for 6.1 too.

Download Liferay Many To Many Portlet from following location

You can find source and war file 


Portlet Screen:


Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.

Once portlet is deployed successfully you can see the portlet in sample category name as Many To Many.

How To use Portlet?

One portlet successfully deployed then portlet available in sample category.

Login as Liferay Admin and add portlet to any page.

Add some students in the database using Add Student Screen


Add some courses in database using Add course screen


 Map Courses to Student from following screen


Map Students to course from following screen


Display students and his/her courses



Author
Meera Prince

Liferay Service Builder Many to Many Relation in Plugin Portlet Part-II

Objective:

Implement Many to Many Relation in liferay development using service builder tool.


Implantation in Portlet Development

The following are steps to implement in portlet development
  1. Create simple Liferay MVC portlet using Liferay IDE in eclipse
  2. Create Service.xml file in Portlet WEB-INF directory
  3. Define required entities in service.xml
  4. Define mapping-table attribute for required entity columns
  5. Run Service builder
  6. Use Generated Data base services

Create simple Liferay MVC portlet using Liferay IDE in Eclipse

Firs we need to create simple Liferay MVC portlet using Liferay IDE in eclipse. This is very simple to create in eclipse.

The following is tutorial from setting liferay environment.

Once you set up the development environment then you can create portlet.

Create service.xml file in Portlet WEB-INF directory

Now we need to create service.xml file in portlet WEB-INF directory. This is the configuration file to service builder tool from this it will generate required service to portlet and these service provide database interaction from portlet.

The following screen shows service.xml file in portlet WEB-INF directory



 Define required entities in service.xml

Now we need to defined required entities in service.xml file so that service builder will generate services.

service.xml has defined tags from which we will define entities these tags information available in service builder DTD file.

The following is service builder DTD we can get more information about service builder tags for service.xml


Assume we have two entities Student and Course

The following is service.xml entities configuration


<?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.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>


Define mapping-table attribute for required entity columns

We need to identify the tables which required Many to Many Relation and then we need to define virtual column for each entity and the column should have mapping-table and entity attributes

The following is example for Student and Course Many to Many Relation


<?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.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>


Note:

 We defined two entities in service.xml but service builder will created 3 tables and the third one is mapping tables which map the student and course primary keys.

Run Service builder

Once we complete the configuration now we need run service builder. We can run service builder in eclipse from ant view. We need click on build-service target from ANT view
In normal ant command for run service builder as follows


ant build-service


The following is ANT build-service in eclipse ant view



Once we run service builder then service builder generates all java classes and configuration files along with SQL scripts.

Service builder generate Model/POJO, Service and Util classes. Mostly we will use Util classes to use service to interact with data bases

The following screen shows Generated classes for Portlet



Use Generated Data base services

Once we generated service builder services then we can use in development

We already know we have used Student and Course entities in service.xml so that we will get many service related to these to tables. We will use Util classes to interact with database.

Add Student


Student student=
StudentLocalServiceUtil.createStudent(CounterLocalServiceUtil.increment());
student.setStudentName(studentName);
student.setStudentPlace(studentPlace);
student.setStudentCollege(studentCollege);
student=StudentLocalServiceUtil.addStudent(student);


Add Course


Course course=CourseLocalServiceUtil.createCourse(CounterLocalServiceUtil.increment());
course.setCourseName(courseName);
course.setCourseGroup(courseGroup);
course=CourseLocalServiceUtil.addCourse(course);


Map Multiple course to one student (this is relation mapping)


Long   studentId=2000;
long[] courseIds=[100,101,102]
CourseLocalServiceUtil.addStudentCourses(studentId, courseIds);
                       

Map Multiple Students to one Course (this is relation mapping)


Long   coursetId=2000;
long[] studensIds=[100,101,102]
StudentLocalServiceUtil.addCourseStudents(courseId, studentsIds);
                                    

Get Student Courses & Get Course students

Get Student Courses


List<Course> studentCourseList= CourseLocalServiceUtil.getStudentCourses(studentId);

for(Student curCourse:studentCourseList){

System.out.println(curCourse.getCourseName());

}


Get Course students


List<Student> courseStudentList=StudentLocalServiceUtil.getCourseStudents(courseId);

for(Student curStudent:courseStudentList){

System.out.println(curStudent.getStudentName());

}


Note:

When we use above methods I got class cast exception so i did work around to display student courses and vice versa.

The following is I posted challenge in Liferay forum


The following is work around get each students courses and each course students

Get Student Courses


List studentCoursesList=CourseLocalServiceUtil.getStudentCourses(studentId);

for(int i=0;i<studentCoursesList.size();i++){

Object curCourse=studentCoursesList.get(i);

JSONObject jobj=JSONFactoryUtil.createJSONObject(curCourse.toString());

System.out.println(jobj.getString("courseName"));

}


Get Course Students


List courseStudentList =StudentLocalServiceUtil.getCourseStudents(courseId);

for(int i=0;i<studentCoursesList.size();i++){

Object curStudent= courseStudentList.get(i);

JSONObject jobj=JSONFactoryUtil.createJSONObject(curStudent.toString());

System.out.println(jobj.getString("studentName"));

}



Important Points

  • Liferay have service builder tool from which we can generate service layer for portlet. Service layer is nothing but set of classes and interfaces.
  • Service builder use spring and hibernate to generate service layer for portlet.
  • Service builder need one configuration file called service.xml file in this we will defined required entities for portlet.
  • Liferay service builder support many to many relation. Mapping-table is an attribute for column from which we can achieve many to may relation between tables.
  • Generally service builder generate required SQL scripts for portlet from the entities which are defined in service.xml
  • When we use mapping-key for columns those columns physically not exited in the table but these columns just virtual column to make Many to Many Relation.
  • Service Builder is create all mapping tables which we used values for mapping-key attribute.

Liferay Service Builder Many to Many Relation in Plugin Portlet Part-I

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.xml by 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.xml file from this information Service Builder generated 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-table attribute 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-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.

Concept Example:

Assume we have Student and 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_Courses is 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.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.


Author
Meera Prince

Recent Posts

Recent Posts Widget

Popular Posts