Liferay Service Builder 6.2:多对一关系

Bre*_*iti 7 liferay liferay-6 liferay-service-builder

我想创建一对多的关系,我使用了以下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>
Run Code Online (Sandbox Code Playgroud)

我的问题是没有为collections方法创建任何东西.没有例外,没有.生成类并且只有简单的getter方法,但没有getCourse().

我做错了什么?

rp.*_*rp. 9

不会自动为您创建getter.每个实体代表数据库中的一个表,因此您必须创建任何有用的getter.幸运的是,如果需要,Service Builder还可以生成此功能.

首先,我们要求Service Builder在Students和之间创建一个映射表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>
Run Code Online (Sandbox Code Playgroud)

接下来,我们创建适当的方法CourseLocalServiceImpl:

public List<Course> getStudentCourses(long studentId)
    throws PortalException, SystemException {

    return coursePersistence.getCourses(studentId);
}
Run Code Online (Sandbox Code Playgroud)

要从对象中获取Courses,Student我们在生成的内部创建方法StudentImpl.java:

public List<Course> getCourses() throws Exceptions {
    return CorseLocalServiceUtil.getStudentCourses(getStudentId());
}
Run Code Online (Sandbox Code Playgroud)

最后,通过运行重新生成类ant build-service.

现在我们可以通过写作获得学生正在学习的所有课程:

List<Course> courses = CourseLocalServiceUtil.getStudentCourses(studentId);
Run Code Online (Sandbox Code Playgroud)

要么

List<Course> courses = student.getCourses();
Run Code Online (Sandbox Code Playgroud)

  • 如果我没有错的话,你的回答显示了OP所要求的多对多关系,而不是一对多关系. (4认同)

Jay*_*edi 6

Liferay在其所有版本中都有指定的文档,这有助于从上到下的方法.

请先参考:

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

对于自发添加以下代码

<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 Code Online (Sandbox Code Playgroud)

运行build-service并在成功执行后,您将看到getter setter方法.