通过配置service.xml 实现
说明:一对多和多对多关系,build-service 后会在persistence层
自动生成contain, count 和 find 方法, 需要手动在service层添加对这些方法的调用。
多对多:
例如 user与Group 的多对多关系, 关联表为Users_Groups
<column name="groups" type="Collection" entity="Group" mapping-table="Users_Groups" />
一对多:
例如skuAttribute与skuAttributeOption之间存在一对多关系
<column name="options" type="Collection" entity="SKUAttributeOption" mapping-key="skuAttributeId" />
自动生成的方法如下
//contain 方法
skuAttributePersistence.containsSKUAttributeOptions(pk);
skuAttributePersistence.containsSKUAttributeOption(pk, skuAttributeOptionPK);
//count 方法
skuAttributePersistence.getSKUAttributeOptionsSize(pk);
//finnd 方法
skuAttributePersistence.getSKUAttributeOptions(pk, start, end);
(一)多对多关系参考代码:
参考 liferay源码\portal-impl\src\com\liferay\portal\service.xml
<entity name="User" uuid="true" local-service="true" remote-service="true">
<!-- PK fields -->
<column name="userId" type="long" primary="true" />
<!-- Audit fields -->
<column name="companyId" type="long" />
<column name="createDate" type="Date" />
<column name="modifiedDate" type="Date" />
<!-- Other fields -->
<column name="defaultUser" type="boolean" />
<column name="contactId" type="long" />
<!-- Relationships -->
<column name="groups" type="Collection" entity="Group" mapping-table="Users_Groups" />
<column name="orgs" type="Collection" entity="Organization" mapping-table="Users_Orgs" />
<!-- Finder methods -->
<!-- References -->
<reference package-path="com.liferay.mail" entity="Mail" />
<reference package-path="com.liferay.portlet.announcements" entity="AnnouncementsDelivery" />
</entity>
(二)一对多关系 参考代码
<entity name="SKUAttribute" local-service="true" remote-service="false">
<column name="skuAttributeId" type="long" primary="true" />
<column name="name" type="String" />
<!-- Relationships -->
<column name="options" type="Collection" entity="SKUAttributeOption" mapping-key="skuAttributeId" />
<!-- order -->
<!-- finder-->
</entity>
<entity name="SKUAttributeOption" local-service="true" remote-service="false">
<column name="skuAttributeOptionId" type="long" primary="true" />
<column name="skuAttributeId" type="long" />
<column name="name" type="String" />
</entity>