Liferay Service Builder 6.2:多对一关系 [英] Liferay Service Builder 6.2: Many to one relationships

查看:111
本文介绍了Liferay Service Builder 6.2:多对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一对多关系,并且使用了以下service.xml:

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>

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

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().

我做错了什么?

推荐答案

不会自动为您创建吸气剂.每个实体代表数据库中的一个表,因此您必须创建任何您认为有用的获取器.幸运的是,如果需要,Service Builder也可以生成此代码.

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.

首先,我们要求Service Builder在StudentsCourses之间创建一个映射表.

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>

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

Next, we create the appropriate method in CourseLocalServiceImpl:

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

    return coursePersistence.getCourses(studentId);
}

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

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());
}

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

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);

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

这篇关于Liferay Service Builder 6.2:多对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆