Liferay Service Builder 6.2: Many to one relationships
Asked Answered
A

2

8

I want to create a one to many relationships and I've 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 gets created for the collections method. No Exception, nothing. The classes are generated and the simple getter methods are there but no getCourse().

What I am doing wrong?

Ander answered 17/6, 2014 at 21:30 Comment(0)
N
9

The getters are not automatically created for you. Each entity represents a table in the database so you'll have to create any getters that you'd find useful. Luckily, Service Builder is also capable of generating this if you need.

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>

Next, we create the appropriate 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 method inside the generated StudentImpl.java:

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

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

Now we can get all the courses a student is taking by writing:

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

or

List<Course> courses = student.getCourses();
Neat answered 17/6, 2014 at 22:48 Comment(4)
Ok, this works. But it leaves us with unwanted database columns (studentId) for the course table, right ?Ander
In the Service Builder XML you've provided above, studentId was already a column of the course table.Neat
ok, true ;) That´s my mistake. Is there a way to get rid of it ? To only have the getter for the List ? And, second question: Is there no way to get those elements from a Student Element ?Ander
Your answer shows many-to-many relationship instead of one-to-many as asked by OP, if I am not wrong.Wimmer
S
6

Liferay in all its version have specified documentation, that helps to move from top to bottom approach.

Please refer this 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

For spontaneous add 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 on successful execution you will see the getter setter methods.

Soileau answered 18/6, 2014 at 5:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.