Terry.Li-彬

虚其心,可解天下之问;专其心,可治天下之学;静其心,可悟天下之理;恒其心,可成天下之业。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  143 随笔 :: 344 文章 :: 130 评论 :: 0 Trackbacks
 

liferay中生成one to many 关系说明

1:在service.xml中配置

For example

<column

       name="shoppingItemPrices"

       type="Collection"

       entity="ShoppingItemPrice"

       mapping-key="itemId"

/>

说明: entitymapping-key属性被指定将建立一个一对多的关系。

2:运行ant build

       生成相关文件

3:在生成的实体实现类中,如ShoppingItemImp.java,新增getShoppingItemPrice()方法

       For example

       public List getItemPrices()

       throws PortalException, SystemException {

/*此处return2种写法

第一种是要求在关系实体中定义根据关联实体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>

posted on 2008-12-18 08:39 礼物 阅读(823) 评论(1)  编辑  收藏 所属分类: Liferay

评论

# re: 在liferay中生成one to many 关系说明 2009-09-09 10:01 买礼物
深奥呀~~  回复  更多评论