Finder in Liferay's ServiceBuilder

Deadly 提交于 2019-12-12 01:42:38

问题


I know I had already asked this question, but I have misunderstandings yet. My previous question: Liferay and relationships in it

In two words: I have a portlet, which can add/update/delete books and add authors. Moreover, you can choose existing authors when you try to add book.

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

And now I need to show how many books were written by each author in "author" table.

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

What finder should I create to achieve my goal? If I create bookName finder Im able to count how many different books I have. If I create authorName finder Im able to count how many authors I have. Im a little lost.

Thank you for your help, but I still have some questions:

  1. How and where can I get authorName with authorId?
  2. How can I use my count variable in my table in 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");   
    }
}


回答1:


In-case, if your Book can have only one Author (many-to-one), then following entity structure will work for you:

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>

On successful build, above finder will generate two methods findByAuthor(long authorId) and countByAuthor(long authorId) in BookUtil. Then, you can implement these methods in BookLocalServiceImpl as following:

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

And on building service once again, you can use these methods in your action class and on view from BookLocalServiceUtil.

Also, you are using JSTL on your views, you need to add dependencies of the jars in liferay-plugin-package.properties as following:

liferay-plugin-package.properties:

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

Your Questions:

  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>


来源:https://stackoverflow.com/questions/39408982/finder-in-liferays-servicebuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!