Liferay 的 ServiceBuilder 中的 Finder

Finder in Liferay's ServiceBuilder

我知道我已经问过这个问题,但我还是有误解。 我之前的问题:

两个字: 我有一个 portlet,可以 add/update/delete 书籍和添加作者。此外,您可以在尝试添加书籍时选择现有作者。

http://i.stack.imgur.com/vzUjn.png

现在我需要显示每个作者在 "author" table 中写了多少本书。

我的service.xml:

<entity name="Book" local-service="true" remote-service="true"  cache-enabled="false">

    <column name="bookId" type="long" primary="true" />
    <column name="bookName" type="String" />
    <column name="bookDescription" type="String" />
    <column name="authors" type="Collection" entity="Author" mapping-table="Books_Authors" />
    <finder return-type="Collection" name="bookName">
        <finder-column name="bookName"></finder-column>
    </finder>

</entity>

<entity name="Author" local-service="true" remote-service="true" cache-enabled="false">
    <column name="authorId" type="long" primary="true" />
    <column name="authorName" type="String" />
    <column name="books" type="Collection" entity="Book" mapping-table="Books_Authors" />

</entity>

我应该创建什么查找器来实现我的目标?如果我创建 bookName finder,我就能数出我有多少本不同的书。如果我创建 authorName finder,我就能计算出我有多少作者。我有点迷茫。

感谢您的帮助,但我还有一些问题:

  1. 如何以及在哪里可以用 authorId 获得 authorName
  2. 如何在 view.jsp 中的 table 中使用我的 count 变量?

    long count = BookLocalServiceUtil.countByAuthor(authorId);
    

    public void addBook(ActionRequest actionRequest, ActionResponse actionResponse) 
        throws IOException, PortletException {

        String bookName = ParamUtil.getString(actionRequest,"bookName");
        String bookDescription = ParamUtil.getString(actionRequest, "bookDescription");
        Long authorId = ParamUtil.getLong(actionRequest, "author");
        try {
        Book book = BookLocalServiceUtil.createBook(CounterLocalServiceUtil.increment());
        book.setBookName(bookName);
        book.setBookDescription(bookDescription);
        book.setAuthorId(authorId);
        book=BookLocalServiceUtil.addBook(book);
        String author = ParamUtil.getString(actionRequest, "authorId");

    } catch (Exception e) {
        log.info(ADD_BOOK_ERROR, e);
        SessionErrors.add(actionRequest, "PortalExceptionError");   
    }
}

在这种情况下,如果您的 Book 只能有一个 Author(多对一),那么以下实体结构将适用于您:

service.xml

<entity name="Book" local-service="true" remote-service="true" cache-enabled="false">
    <column name="bookId" type="long" primary="true"></column>
    <column name="bookName" type="String"></column>
    <column name="bookDescription" type="String"></column>
    <column name="authorId" type="long"></column>

    <finder return-type="Collection" name="Author">
        <finder-column name="authorId"></finder-column>
    </finder>
</entity>
<entity name="Author" local-service="true" remote-service="true" cache-enabled="false">
    <column name="authorId" type="long" primary="true"></column>
    <column name="authorName" type="String"></column>
</entity>

构建成功后,上面的finder会在BookUtil中生成两个方法findByAuthor(long authorId)countByAuthor(long authorId)。然后,您可以在 BookLocalServiceImpl 中实现这些方法,如下所示:

BookLocalServiceImpl:

public List<Book> findByAuthor(long authorId) {
    try {
        return BookUtil.findByAuthor(authorId);
    } catch (Exception ex) {}

    return null;
}

public int countByAuthor(long authorId) {
    try {
        return BookUtil.countByAuthor(authorId);
    } catch (Exception ex) {}

    return 0;
}

再次构建服务时,您可以在操作中使用这些方法 class 并在 BookLocalServiceUtil 的视图中使用这些方法。

此外,您在视图上使用了 JSTL,您需要添加 liferay-plugin-package.properties 中的 jar 依赖项,如下所示:

liferay-plugin-package.properties:

portal-dependency-jars=\
    jstl-api.jar,\
    jstl-impl.jar

您的问题:

  1. How and where can I get authorName with authorId?
<liferay-ui:search-container>
    <liferay-ui:search-container-results results="${bookListArray}" />
    <liferay-ui:search-container-row className="builder.model.Book"
        keyProperty="bookId" modelVar="aBook">
        <liferay-ui:search-container-column-text property="bookName"
            name="book-Name" />
        <liferay-ui:search-container-column-text property="bookDescription"
            name="description" />
        <%
            Author bookAuthor = AuthorLocalServiceUtil.getAuthor(aBook.getAuthorId());
        %>
        <liferay-ui:search-container-column-text name="Author"
            value="<%=bookAuthor.getAuthorName()  %>" />
        <liferay-ui:search-container-column-jsp path="/html/actionBook.jsp" />
    </liferay-ui:search-container-row>
    <liferay-ui:search-iterator />
</liferay-ui:search-container>
  1. How can I use my count variable in my table in view.jsp?
<liferay-ui:search-container>
    <liferay-ui:search-container-results results="${authorListArray}" />
    <liferay-ui:search-container-row className="builder.model.Author"
        keyProperty="authorId" modelVar="aAuthor">
        <liferay-ui:search-container-column-text property="authorName"
            name="author-Name" />
        <%
            int count = BookLocalServiceUtil.countByAuthor(aAuthor.getAuthorId());
        %>
        <liferay-ui:search-container-column-text name="count"
            value="<%=String.valueOf(count)  %>" />
        <liferay-ui:search-container-column-jsp path="/html/actionAuthor.jsp" />
    </liferay-ui:search-container-row>
    <liferay-ui:search-iterator />
</liferay-ui:search-container>