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:
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:
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
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…
Found it…
int maxLength = ModelHintsUtil.getMaxLength(klass.getName(), name);
Hi, this should generate a validation on the View and Model? Or only view?