首页 > 代码库 > Liferay Model Hints

Liferay Model Hints

这篇文章主要介绍如何通过model hint 来修改liferay builder service生成数据库表中默认字段的长度。

1.什么是Model Hints?

它是对liferay builder service生成的model类的基本提示.

 

2. 它在什么地方被定义的?

portlet-model-hints.xml

 

3. 文件位置?

/docroot/WEB-INF/src/META-INF

 

 

4. 为什么被称为Model Hints?

Liferay把它命名为Model Hints,因为Model Hints表明了实体呈现给用户的方式,同时也指定了实体的字段在数据库中占的内存大小。

 

5. 我能在portal源码中找到关于model hints的更多详细信息吗?

能,在Liferay 7.0 ga4中, model hints的文件位于liferay-ce-portal-7.0-ga4/tomcat-8.0.32/webapps/ROOT/WEB-INF/lib/portal-impl/META-INF/portal-model-hints.xml

 

6. Model Hints的最佳用处是什么?

用来定义存储在数据库中实体的字段的size.

 

现在让我们来看一下实例(改变service builder在数据库生成的表的字段长度)

1. service.xml

<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_7_0_0.dtd">

<service-builder package-path="my.test.sb">
    <namespace>FOO</namespace>
    <!--<entity data-source="sampleDataSource" local-service="true" name="Foo" remote-service="false" session-factory="sampleSessionFactory" table="foo" tx-manager="sampleTransactionManager uuid="true"">-->
    <entity local-service="true" name="Foo" remote-service="true" uuid="true">

        <!-- PK fields -->

        <column name="fooId" primary="true" type="long" />

        <!-- Group instance -->

        <column name="groupId" type="long" />

        <!-- Audit fields -->

        <column name="companyId" type="long" />
        <column name="userId" type="long" />
        <column name="userName" type="String" />
        <column name="createDate" type="Date" />
        <column name="modifiedDate" type="Date" />

        <!-- Other fields -->

        <column name="field1" type="String" />
        <column name="field2" type="boolean" />
        <column name="field3" type="int" />
        <column name="field4" type="Date" />
        <column name="field5" type="String" />

        <!-- Order -->

        <order by="asc">
            <order-column name="field1" />
        </order>

        <!-- Finder methods -->

        <finder name="Field2" return-type="Collection">
            <finder-column name="field2" />
        </finder>

        <!-- References -->

        <reference entity="AssetEntry" package-path="com.liferay.portlet.asset" />
        <reference entity="AssetTag" package-path="com.liferay.portlet.asset" />
    </entity>
</service-builder>

2.生成的portlet-model-hints.xml

<?xml version="1.0"?>

<model-hints>
    <model name="my.test.sb.model.Foo">
        <field name="uuid" type="String" />
        <field name="fooId" type="long" />
        <field name="groupId" type="long" />
        <field name="companyId" type="long" />
        <field name="userId" type="long" />
        <field name="userName" type="String" />
        <field name="createDate" type="Date" />
        <field name="modifiedDate" type="Date" />
        <field name="field1" type="String" />
        <field name="field2" type="boolean" />
        <field name="field3" type="int" />
        <field name="field4" type="Date" />
        <field name="field5" type="String" />
    </model>
</model-hints>

3. 在改变model hints之前数据库中字段的长度

技术分享

只是userName的长度是75

4. 现在改变portlet-model-hints.xml文件,内容如下。把useName的长度改为100

<?xml version="1.0"?>

<model-hints>
    <model name="my.test.sb.model.Foo">
        <field name="uuid" type="String" />
        <field name="fooId" type="long" />
        <field name="groupId" type="long" />
        <field name="companyId" type="long" />
        <field name="userId" type="long" />
        <field name="userName" type="String">
            <hint name="max-length">100</hint>
        </field>
        <field name="createDate" type="Date" />
        <field name="modifiedDate" type="Date" />
        <field name="field1" type="String" />
        <field name="field2" type="boolean" />
        <field name="field3" type="int" />
        <field name="field4" type="Date" />
        <field name="field5" type="String" />
    </model>
</model-hints>

5. 运行cleanServiceBuilderTask(参照之前写的清理service builder产生的table文)之后数据库如下图所示,userName的长度变成了100.

技术分享

然后大功告成,更多详细信息http://proliferay.com/liferay-model-hints/

https://dev.liferay.com/zh/develop/tutorials/-/knowledge_base/6-2/customizing-model-entities-with-model-hints#related-topics

 

Liferay Model Hints