在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>