• Liferay Model Hints

    Posted on October 2, 2014 by Hamidul Islam in Liferay.

    Model Hints

    Aim of this Article:

    In this article I will show how we can define our own column size with the help of model hints. Model hints are associated with entity which is  generated by Liferay Service Builder.

     

    1.  What's Model Hints?

    Answer:-

    Model hints are basically hints to model class generated by Liferay Service Builder.


     

    2.  In which file its defined?

    Answer:-

    Its defined in portlet-model-hints.xml.


     

    3.  What's the location of model hints file?

    Answer:-

    If you have used service builder in your portlet then its available in /docroot/WEB-INF/src/META-INF folder.


    4.  Why its called Model Hints?

    Answer:-

    Liferay calls them Model Hints because they suggest how entities should be presented to users and can also specify the size of database columns used to store the entities.


    5.  In portal source can I get more details about model hints?

    Answer:-

    Yes. You can get more idea about model hints from portal source code. In liferay 6.2 the model hints file location is liferay-portal-src-6.2.0-ce-ga1/portal-impl/src/META-INF/portal-model-hints.xml.


    6.  Whats the best example of Model Hints usage?

    Answer:-

    Define the size of database columns used to store the entities.


     

    Lets change column length

     

    Sample service.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
    <service-builder package-path="com.proliferay.servicebuilder">
        <author>hamidul</author>
        <namespace>BOOK</namespace>
    
        <entity name="Book" table="BOOK" local-service="true" remote-service="true">
            <column name="bookId" type="long" primary="true" />
            <column name="bookName" type="String" />
            <column name="authorName" type="String" />
            <column name="bookPrice" type="int" />
            <column name="isbn" type="int" />
        </entity>
    </service-builder>
    

    Generated portlet-model-hints.xml:

    <?xml version="1.0"?>
    <model-hints>
        <model name="com.proliferay.servicebuilder.model.Book">
            <field name="bookId" type="long" />
            <field name="bookName" type="String" />
            <field name="authorName" type="String" />
            <field name="bookPrice" type="int" />
            <field name="isbn" type="int" />
        </model>
    </model-hints>
    

    Database screen shot before changing model hints:

    Model Hints

    How to change column length:

    Modify portlet-model-hints.xml file as below. Here we are defining the column length of bookName  to 100.

    <?xml version="1.0"?>
    
    <model-hints>
        <model name="com.proliferay.servicebuilder.model.Book">
            <field name="bookId" type="long" />
            <field name="bookName" type="String">
                <hint name="max-length">100</hint>
            </field>
            <field name="authorName" type="String" />
            <field name="bookPrice" type="int" />
            <field name="isbn" type="int" />
        </model>
    </model-hints>
    

    Note: You should rebuild the service and deploy the portlet to see the changes.

    Database screen shot after changing model hints:

    Model Hints

    In this article we have changed only the column length. In fact you can do a lot more with model hints. Below is the complete list of Model Hint Values and Descriptions

    Model Hints

    https://www.liferay.com/documentation/liferay-portal/6.2/development/-/ai/use-model-hints-liferay-portal-6-2-dev-guide-04-en

    Post Tagged with ,

3 Responses so far.

  1. pshab22 says:

    Hi thanks for the info. Is there any LR service-util class to look up the configured column length (useful for validation)? Otherwise we have to parse the file or the serviceComponent entry in the table...

  2. pshab22 says:

    Found it...
    int maxLength = ModelHintsUtil.getMaxLength(klass.getName(), name);

  3. Miguel Saraiva says:

    Hi, this should generate a validation on the View and Model? Or only view?

Top
%d bloggers like this: