How can I check entity restrictions while saving an object in sleep mode

I have an entity with a field name, and I want it to be no more than 255, so I defined it like this:

@Entity
public class A implements Serializable {

...

@NotNull
@Size(max=255)
private String name;

I want it to be checked since I call a.persist (), so if the name is too long an exception is thrown.

I have a HibernateValidator defined in validation.xml:

<?xml version="1.0" encoding="UTF-8"?>
<validation-config
     xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd">
 <default-provider>org.hibernate.validator.HibernateValidator</default-provider>
 <message-interpolator>org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator</message-interpolator>
<traversable-resolver>org.hibernate.validator.engine.resolver.DefaultTraversableResolver</traversable-resolver>
<constraint-validator-factory>org.hibernate.validator.engine.ConstraintValidatorFactoryImpl</constraint-validator-factory>
</validation-config>

But that will not work. During saving, an exception does not occur, and only during the commit, when the object manager is cleared, I get an exception, and even then this is an exception from the database (since it also has a column limit of 255). Therefore, I believe that my check does not work at all.

, : 1) , 2) , ?

+5
1

Hibernate validator.

BeanValidationEventListener.

.

, .

+4

All Articles