Liferay Service Builder 6.2: Many-to-One Relationships

I want to create a one-to-many relationship, and I used the following service.xml:

<entity name="Student" local-service="true" remote-service="true" cache-enabled="false"> <column name="studentId" type="long" primary="true" /> <column name="courses" type="Collection" entity="Course"/> </entity> <entity name="Course" local-service="true" remote-service="true" cache-enabled="false"> <column name="courseId" type="long" primary="true" /> <column name="studentId" type="long"/> </entity> 

My problem is that nothing is being created for the collection method. There is no exception, nothing. Classes are created and simple getter methods exist, but no getCourse ().

What am I doing wrong?

+6
source share
2 answers

Recipients are not automatically created for you. Each object represents a table in the database, so you will need to create any getters that you find useful. Fortunately, Service Builder is also able to generate this if you need to.

First, we ask Service Builder to create a mapping table between Students and Courses .

 <entity name="Student" local-service="true" remote-service="true" cache-enabled="false"> <column name="studentId" type="long" primary="true" /> <column name="courses" type="Collection" entity="Course" mapping-table="Courses_Students" /> </entity> <entity name="Course" local-service="true" remote-service="true" cache-enabled="false"> <column name="courseId" type="long" primary="true" /> <column name="students" type="Collection" entity="Student" mapping-table="Courses_Students" /> </entity> 

Then we will create the corresponding method in CourseLocalServiceImpl :

 public List<Course> getStudentCourses(long studentId) throws PortalException, SystemException { return coursePersistence.getCourses(studentId); } 

To get Courses from the Student object, we create a method inside the generated StudentImpl.java :

 public List<Course> getCourses() throws Exceptions { return CorseLocalServiceUtil.getStudentCourses(getStudentId()); } 

Finally, restore your classes by running ant build-service .

Now we can get all the courses that the student takes by writing:

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

or

 List<Course> courses = student.getCourses(); 
+7
source

Liferay in its entire version has the specified documentation, which helps to move from the approach from top to bottom.

Please refer to the first:

https://www.liferay.com/documentation/liferay-portal/6.2/development/-/ai/define-your-object-relational-map-liferay-portal-6-2-dev-guide-04-en

To spontaneously add the following code

 <entity name="Student" local-service="true" remote-service="true" cache-enabled="false"> <column name="studentId" type="long" primary="true" /> <column name="courses" type="Collection" entity="Course"/> </entity> <entity name="Course" local-service="true" remote-service="true" cache-enabled="false"> <column name="courseId" type="long" primary="true" /> <column name="studentId" type="long"/> <finder name="courseId" return-type="Collection"> <finder-column name="courseId" /> </finder> <finder name="studentId" return-type="Collection"> <finder-column name="studentId" /> </finder> </entity> 

Run build-service and if successful, you will see the methods for setting the recipient.

+6
source

Source: https://habr.com/ru/post/970971/


All Articles