问题描述
我有一个Portlet,可以添加/更新/删除书籍并添加作者.此外,当您尝试添加图书时,可以选择现有的作者.
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.
现在我需要在作者"表中显示每个作者写了多少本书.我该怎么做?我是liferay的新手,我什至不知道.
And now I need to show how many books were written by each author in "author" table. How can I do it? Im a newbie in liferay and I even have no idea.
这是我的service.xml
It's 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>
推荐答案
Service Builder是您的朋友.
Service Builder is your friend.
您只需要在service.xml
中的书本实体中添加一个查找器.如果您的实体有一个名为author
:
You just need to add a finder in your book entity in service.xml
. If your entity has a field named author
:
<finder name="Author" return-type="Collection">
<finder-column name="author" />
</finder>
执行构建服务将生成方法BookUtil.findByAuthor()
和BookUtil.countByAuthor()
.
The execution of build-service will generate the methods BookUtil.findByAuthor()
and BookUtil.countByAuthor()
.
您现在可以在BookLocalServiceImpl
中实现相应的方法,调用先前的方法,然后在再次运行build-serivce之后,这些方法在您的Util
类中可用.像
You can implement now the corresponding methods in BookLocalServiceImpl
, calling the previous, and after another run of build-serivce, they're available in your Util
class. Something like
public List<Book> getByAuthor(String author) {
return getPersistence().findByAuthor(author);
}
public int countByAuthor(String author) {
return getPersistence().countByAuthor(author);
}
上次致电构建服务后,您可以从BookLocalServiceUtil
致电他们.
After the last call to build-service you can call them from your BookLocalServiceUtil
.
如果只想计数,请不要检索所有集合.如果记录很多,那是个坏主意.改为调用计数.
If you just want the count, don't retrieve all the collection. If there are many records, it's a bad idea. Invoke the count instead.
这篇关于Liferay及其中的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!