springhibernatevalidationnhibernate-validatorurl-validation

Hibernate validator URL and ConstraintViolationException


I'm using Hibernate validator 4.3. I try validate given entity:

@Entity
@Table(name = "standard_group")
    public class StandardGroup extends AbstractEntity {

    private static final long serialVersionUID = 1985481354L;
    private Long id;
    private String groupName;
    private String commissionDecisionFileUrl;
    private String urlTitle;

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @NotEmpty(message = "custom error text")
    @Column(name = "grup_name")
    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    @URL(message = "custom error text") 
    @Column(name = "comission_decision_file_url")
    public String getCommissionDecisionUrl() {
        return commissionDecisionFileUrl;
    }

    public void setCommissionDecisionUrl(String commissionDecisionUrl) {
        this.commissionDecisionFileUrl = commissionDecisionUrl;
    }

    @Column(name = "url_title", length = 25)
    @Length(max = 25, message = "custom error text")
    public String getUrlTitle() {
        return urlTitle;
    }

    public void setUrlTitle(String urlTitle) {
        this.urlTitle = urlTitle;
    }

But I have problem in web form, when commissionDecisionFileUrl empty and urlTitle is filled, then get Exception (otherwise validation process works fine):

javax.validation.ConstraintViolationException: Validation failed for classes [sk.peterjurkovic.cpr.entities.StandardGroup] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='Odkaz na soubor ma chybný tvar', propertyPath=commissionDecisionUrl, rootBeanClass=class sk.peterjurkovic.cpr.entities.StandardGroup, messageTemplate='Odkaz na soubor ma chybný tvar'}
]
    org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159)
    org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreUpdate(BeanValidationEventListener.java:102)
    org.hibernate.action.internal.EntityUpdateAction.preUpdate(EntityUpdateAction.java:235)
    org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:115)
    org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
    org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:276)
    org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
    org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)

Thanks you for help.


Solution

  • Based on the information given it is hard to tell for certain. I suggest you set a breakpoint in org.hibernate.validator.constraints.impl.URLValidator.isValid(String, ConstraintValidatorContext) and see for yourself why the input is not valid.