2014-06-17 20 views
6

Tôi muốn tạo ra một đến nhiều mối quan hệ và tôi đã sử dụng các service.xml sau:Dịch vụ Liferay Builder 6.2: Nhiều người đến một mối quan hệ

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

Vấn đề của tôi là không có gì được tạo ra cho các phương pháp bộ sưu tập . Không ngoại lệ, không có gì. Các lớp được tạo ra và các phương thức getter đơn giản có nhưng không có getCourse().

Tôi đang làm gì sai?

Trả lời

7

Getters không được tạo tự động cho bạn. Mỗi thực thể đại diện cho một bảng trong cơ sở dữ liệu, do đó bạn sẽ phải tạo bất kỳ getters nào mà bạn thấy hữu ích. May mắn thay, Service Builder cũng có khả năng tạo ra điều này nếu bạn cần.

Trước tiên, chúng tôi yêu cầu Trình tạo dịch vụ tạo bảng ánh xạ giữa StudentsCourses.

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

Tiếp theo, chúng ta tạo ra các phương pháp thích hợp trong CourseLocalServiceImpl:

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

    return coursePersistence.getCourses(studentId); 
} 

Để có được Courses từ đối tượng Student chúng ta tạo ra phương pháp bên trong tạo ra StudentImpl.java:

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

Cuối cùng, tái tạo của bạn các lớp học bằng cách chạy ant build-service.

Bây giờ chúng ta có thể nhận được tất cả các khóa học một sinh viên đang tiến hành bằng cách viết:

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

hoặc

tài liệu
List<Course> courses = student.getCourses(); 
+0

Ok, tính năng này hoạt động. Nhưng nó để lại cho chúng ta các cột cơ sở dữ liệu không mong muốn (studentId) cho bảng khóa học, đúng không? – Breiti

+0

Trong XML bộ dựng dịch vụ mà bạn đã cung cấp ở trên, 'studentId' đã là một cột của bảng' course'. –

+0

ok, đúng;) Đó là sai lầm của tôi. Có cách nào để loại bỏ nó không? Để chỉ có getter cho Danh sách? Và, câu hỏi thứ hai: Không có cách nào để có được những yếu tố đó từ một phần tử sinh viên? – Breiti

6

Liferay trong tất cả các phiên bản của nó đã xác định, giúp di chuyển từ trên xuống dưới tiếp cận.

Vui lòng tham khảo này đầu tiên:

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

Đối với thêm tự phát mã sau

<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 xây dựng-dịch vụ và thực hiện thành công bạn sẽ thấy những phương pháp getter setter.

Các vấn đề liên quan