0

我正在使用 Hibernate 验证器 4.3。我尝试验证给定的实体:

@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;
    }

但是我在 web 表单中遇到问题,当commissionDecisionFileUrl为空并且urlTitle被填充时,然后得到异常(否则验证过程可以正常工作):

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)

谢谢你的帮助。

4

1 回答 1

2

根据所提供的信息,很难确定。我建议您设置一个断点org.hibernate.validator.constraints.impl.URLValidator.isValid(String, ConstraintValidatorContext)并自己查看输入无效的原因。

于 2013-01-01T20:13:44.663 回答