0

I need to know the equivalent of @size annotation used in hibernate model classes, which we can use in the hbm.xml file.

Little background : So I ran into this problem where a column size is specified as 255 in the database and I have restricted the characters in corresponding input box to 255 using the maxLength attribute of the textArea html tag. User is not able to enter more than 255 characters. But, when you enter certain gibberish text in the text box which contains 255 charcters only, the code breaks at the time of saving it. Logs say that we are trying to enter 256 characters! Example text entered :

lkjsajf,asdmcladskfhncv,sd.mcvjhsdlk;j;lsdnc;;sadkjlfhsdnvcjsdhfvdsnkcjlsdjlfcujdslknfjkds;hgdsjnfajijujjfkvlfdsvil;djsvmjds;ocujas;mcklsdavhodsalvnlsdka;hvdslnvc;lsidhvydsknvlioadsiugvpodsjv;sdaou'gvdpsovjl;cs;llhavfaskgjvlkds'jvc'dsajouopds 'fjdls;jvcl;

To fix this, I added @size annotation on the corresponding column of model, which is used by hibernate bean validation and prevents exception being thrown on the page and shows the validation message instead. My project has mixture of hbm mapping files and just the model classes with hibernate annotations. I need to know what to use in the hbm file to show the same validation message.

OcelotcR
  • 171
  • 4
  • 18
rj4u
  • 87
  • 10

1 Answers1

0

Assuming that you're unable to refactor your code to switch completely to annotation (because obviously that way your code would be much readable and maintainable), I think you can use the bean validation implementation of the hibernate framework using xml configuration. You should first add META-INF/validation.xml file to your final artifact. It would have an structure like this:

<validation-config
        xmlns="http://xmlns.jcp.org/xml/ns/validation/configuration"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/configuration
            http://xmlns.jcp.org/xml/ns/validation/configuration/validation-configuration-2.0.xsd"
        version="2.0">

    <default-provider>com.acme.ValidationProvider</default-provider>

    ...
    <constraint-mapping>META-INF/validation/constraints-car.xml</constraint-mapping>
    ...

</validation-config>

You can take a look at its documentation for details of its schema. An important element inside that file is the <constraint-mapping> which allows you to define the address of the file that contains your actual constraint definitions. Inside that you can define your constraints like this:

<constraint-mappings
        xmlns="http://xmlns.jcp.org/xml/ns/validation/mapping"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/mapping
            http://xmlns.jcp.org/xml/ns/validation/mapping/validation-mapping-2.0.xsd"
        version="2.0">

    <default-package>com.your.project.model.package</default-package>
    <bean class="Person" ignore-annotations="true">
        <field name="firstName">
            <constraint annotation="javax.validation.constraints.Size">
                <element name="max">80</element>
            </constraint>
        </field>
    </bean>
</constraint-mappings>

I didn't run this code, but I think you got the idea ;)

zaerymoghaddam
  • 3,037
  • 1
  • 27
  • 33