2014-06-17 9 views
6

Chcę utworzyć jeden do wielu relacji i Użyłem następujący service.xml:Liferay Usługa Builder 6.2: wiele do jednego relacji

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

mój problem jest, że nic nie zostanie stworzony dla metody zbiorów . Żadnego wyjątku, nic. Klasy są generowane i istnieją proste metody pobierające, ale nie ma metody getCourse().

Co robię źle?

Odpowiedz

7

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(); 
+0

OK, to działa. Ale pozostawia nam niechciane kolumny bazy danych (studentId) dla tabeli kursów, prawda? – Breiti

+0

W powyższym kodzie XML konstruktora usług, 'studentId' był już kolumną tabeli" kurs ". –

+0

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

6

Liferay we wszystkich jego wersji nie określonej dokumentacji, która pomaga przenieść się z góry do dołu podejście.

Proszę odnieść się ten pierwszy:

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

spontanicznych Dodaj następujący kod

<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 gromadzeniu usługę i po pomyślnym wykonaniu widać metody getter setter.

Powiązane problemy