Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set size of string column using service.xml or Service Builder?

Pre-history: I get JDBCExceptionReporter data exception: string data, right truncation exception on update of entities.

I have found that this means, the data is too big for the specified varchar.

In the service.xml the column is specified like:

<column name="message" type="String"/>

I have found in Liferay's source code for ServiceBuilder this fragment:

else if (colType.equals("String")) {
    Map<String, String> hints = ModelHintsUtil.getHints(
        _packagePath + ".model." + entity.getName(), colName);

    int maxLength = 75;

    if (hints != null) {
        maxLength = GetterUtil.getInteger(
            hints.get("max-length"), maxLength);
    }

    if (col.isLocalized()) {
        maxLength = 4000;
    }

    if (maxLength < 4000) {
        sb.append("VARCHAR(" + maxLength + ")");
    }
    else if (maxLength == 4000) {
        sb.append("STRING");
    }
    else if (maxLength > 4000) {
        sb.append("TEXT");
    }
}

Now my question is, how can I define the max-length for my columns?

like image 577
Mark Avatar asked Aug 16 '12 15:08

Mark


1 Answers

You need to modify your portlet-model-hints.xml file that you should find under:

docroot/WEB-INF/src/META-INF/portlet-model-hints.xml

In here you can define hint values such as:

<hint-collection name="TEXTAREA">
    <hint name="max-length">500</hint>
</hint-collection>

Then find the column(s) you want to apply this hint in the same file and it should look like this:

<field name="your_column_name" type="String">
    <hint-collection name="TEXTAREA" />
</field>

or you can as well directly write like this, if there is just one column with the specified hint of max-length:

<field name="your_column_name" type="String">
    <hint name="max-length">500</hint>
</field>

Then you need to run build-service again and when you deploy your portlet your DB table should be updated to reflect this change.

I hope this answers your question!

There are other hints and a list can be found in this wiki, I just showed max-length here as it sounds like that's the one you need.

The complete file would look something like the following if you had one table, with one column. Yours will most likely be a lot longer!

<?xml version="1.0"?>
<model-hints>
    <hint-collection name="TEXTAREA">
        <hint name="max-length">500</hint>
    </hint-collection>
    <model name="com.mynamespace.model.MyModelClass">
        <field name="myColumn" type="String">
            <hint-collection name="TEXTAREA" />
        </field>
    </model>
</model-hints>
like image 143
Jonny Avatar answered Sep 21 '22 12:09

Jonny