Moduły pobierające nie są tworzone automatycznie. Każda encja reprezentuje tabelę w bazie danych, więc będziesz musiał utworzyć wszystkie pobrania, które uznasz za przydatne. Na szczęście Konstruktor usług może również wygenerować tę funkcję, jeśli zajdzie taka potrzeba.
Najpierw poprosimy Konstruktora usług, aby utworzył tabelę odwzorowań między Students
i 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>
Następnie tworzymy odpowiednią metodę w CourseLocalServiceImpl
:
public List<Course> getStudentCourses(long studentId)
throws PortalException, SystemException {
return coursePersistence.getCourses(studentId);
}
Aby uzyskać Courses
z obiektu Student
tworzymy metodę wewnątrz generowanego StudentImpl.java
:
public List<Course> getCourses() throws Exceptions {
return CorseLocalServiceUtil.getStudentCourses(getStudentId());
}
Wreszcie zregenerować klasy, uruchamiając ant build-service
.
Teraz możemy uzyskać wszystkie kursy student biorąc pisząc:
List<Course> courses = CourseLocalServiceUtil.getStudentCourses(studentId);
lub
List<Course> courses = student.getCourses();
OK, to działa. Ale pozostawia nam niechciane kolumny bazy danych (studentId) dla tabeli kursów, prawda? – Breiti
W powyższym kodzie XML konstruktora usług, 'studentId' był już kolumną tabeli" kurs ". –
ok, prawda;) To mój błąd. Czy istnieje sposób, aby się go pozbyć? Aby zdobyć tylko listę dla listy? I drugie pytanie: czy nie ma sposobu na odzyskanie tych elementów z elementu uczniowskiego? – Breiti