在liferay中生成one to many 關系說明
1:在service.xml中配置
For example:
<column
name="shoppingItemPrices"
type="Collection"
entity="ShoppingItemPrice"
mapping-key="itemId"
/>
說明: entity和mapping-key屬性被指定將建立一個一對多的關系。
2:運行ant build
生成相關文件
3:在生成的實體實現類中,如ShoppingItemImp.java,新增getShoppingItemPrice()方法
For example:
public List getItemPrices()
throws PortalException, SystemException {
/*此處return有2種寫法
第一種是要求在關系實體中定義根據關聯實體id進行查詢的方法,如:
*/
return ShoppingItemPriceLocalServiceUtil.getItemPrices(getItemId());
/*
第二種是通過業務層代理類獲的實體的持久類實例進行操作
*/
return ActiveEntryLocalServiceUtil.getActiveEntryPersistence().getCheckItemEntries(this.getActiveId());
}
4:再次運行 ant build
如此便可在使用實體的時候直接獲取關聯對象實體
如:
List list = ActiveEntry.getCheckItemEntries ();
如果要在hbm配置文件中生成一對多關系描述,則需修改freemarker模板文件
文件路徑:com/liferay/portal/tool/servicebuilder/dependencies/hbm_xml.flt
(如果對其他生成文件內容做調整,也可修改其他對應的模板文件)
修改為如下內容(也可直接copy)
<#list entities as entity>
<#if entity.hasColumns()>
<class name="${packagePath}.model.impl.${entity.name}Impl" table="${entity.table}">
<cache usage="read-write" />
<#if entity.hasCompoundPK()>
<composite-id name="primaryKey" class="${packagePath}.service.persistence.${entity.name}PK">
<#assign pkList = entity.getPKList()>
<#list pkList as column>
<key-property name="${column.name}"
<#if column.name != column.DBName>
column="${column.DBName}"
</#if>
/>
</#list>
</composite-id>
<#else>
<#assign column = entity.getPKList()?first>
<id name="${column.name}"
<#if column.name != column.DBName>
column="${column.DBName}"
</#if>
type="<#if !entity.hasPrimitivePK()>java.lang.</#if>${column.type}">
<#if column.idType??>
<#assign class = serviceBuilder.getGeneratorClass("${column.idType}")>
<#if class == "class">
<#assign class = column.IdParam>
</#if>
<#else>
<#assign class = "assigned">
</#if>
<generator class="${class}"
<#if class == "sequence">
<param name="sequence">${column.IdParam}</param>
</generator>
<#else>
/>
</#if>
</id>
</#if>
<#list entity.columnList as column>
<#if column.EJBName??>
<#assign ejbName = true>
<#else>
<#assign ejbName = false>
</#if>
<#if !column.isPrimary() && !column.isCollection() && !ejbName>
<property name="${column.name}"
<#if column.name != column.DBName>
column="${column.DBName}"
</#if>
<#if column.isPrimitiveType() || column.type == "String">
type="com.liferay.util.dao.hibernate.${serviceBuilder.getPrimitiveObj("${column.type}")}Type"
</#if>
/>
</#if>
<#if !column.isPrimary() && column.isCollection()>
<#if column.isMappingOneToMany()>
<set name="${column.name}" lazy="true">
<key column="${column.getMappingKey()}"/>
<one-to-many class="${packagePath}.service.persistence.${column.EJBName}" />
</set>
</#if>
</#if>
</#list>
</class>
</#if>
</#list>