0

我知道我已经问过这个问题,但我还有误解。我之前的问题: Liferay 和其中的关系

简而言之:我有一个 portlet,它可以添加/更新/删除书籍和添加作者。此外,您可以在尝试添加书籍时选择现有作者。

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

现在我需要在“作者”表中显示每个作者写了多少本书。

我的服务.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. authorName我如何以及在哪里可以获得authorId
  2. 如何count在我的表中使用我的变量view.jsp

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

在此处输入图像描述

4

1 回答 1

1

万一,如果您Book只能拥有一个Author(多对一),那么以下实体结构将适合您:

服务.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 将生成两个方法findByAuthor(long authorId),并countByAuthor(long authorId)BookUtil. 然后,您可以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;
}

在再次构建服务时,您可以在您的操作类中使用这些方法并从BookLocalServiceUtil.

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

liferay-plugin-package.properties:

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

你的问题:

  1. authorName我如何以及在哪里可以获得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. 如何在我的表中使用我的计数变量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>
于 2016-09-09T12:25:13.643 回答