0

すでにこの質問をしたことは知っていますが、まだ誤解があります。私の前の質問: Liferay とその中の関係

一言で言えば、書籍の追加/更新/削除と著者の追加ができるポートレットがあります。さらに、本を追加しようとすると、既存の著者を選択できます。

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

    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

場合によっては、 Book1 つしか持てない場合Author(多対 1)、次のエンティティ構造が機能します。

サービス.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 は 2 つのメソッドfindByAuthor(long authorId)countByAuthor(long authorId)inを生成し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か?
<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 に答える