Liferay的ServiceBuilder中的Finder

时间:2016-09-09 10:04:20

标签: liferay liferay-6 relationships liferay-service-builder

我知道我已经问过这个问题了,但我还有误解。 我之前的问题: Liferay and relationships in it

用两个词说: 我有一个portlet,可以添加/更新/删除书籍和添加作者。此外,您可以在尝试添加图书时选择现有作者。

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

现在我需要展示每位作者在"作者"表

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

    long count = BookLocalServiceUtil.countByAuthor(authorId);
    
  3.     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");   
        }
    }
    

    enter image description here

1 个答案:

答案 0 :(得分:1)

如果您的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>

成功构建后,上方查找器将在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,需要在liferay-plugin-package.properties中添加jar的依赖关系,如下所示:

liferay-plugin-package.properties:

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

您的问题:

  
      
  1. authorName如何以及在哪里获得authorId
  2.   
<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
  2. 的表格中使用我的计数变量   
<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>