Liferay 和其中的关系

Liferay and relationships in it

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

现在我需要显示每个作者在 "author" table 中写了多少本书。我该怎么做?我是liferay的新手,完全不懂

这是我的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>

Service Builder 是您的朋友。

您只需要在 service.xml 中的图书实体中添加一个查找器。如果您的实体有一个名为 author:

的字段
<finder name="Author" return-type="Collection">
    <finder-column name="author" />
</finder>

build-service的执行会生成方法BookUtil.findByAuthor()BookUtil.countByAuthor().

您现在可以在BookLocalServiceImpl中实现相应的方法,调用之前的,在另一个运行 build-serivce之后,它们在您的Util [=31]中可用=].像

public List<Book> getByAuthor(String author) {
    return getPersistence().findByAuthor(author);
}

public int countByAuthor(String author) {
    return getPersistence().countByAuthor(author);
}

最后一次调用构建服务后,您可以从 BookLocalServiceUtil.

中调用它们

如果您只想要计数,请不要检索所有集合。如果有很多记录,这是一个坏主意。改为调用计数。