How can I validate constrains on entities during persist of an entity in hibernate

I have an entity with a field name, and I want it to be not longer 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 validated as I call a.persist(), so that if name is too long an exception is thrown. I have HibernateValidator defined in validation.xml: org.hibernate.validator.HibernateValidator org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator org.hibernate.validator.engine.resolver.DefaultTraversableResolver org.hibernate.validator.engine.ConstraintValidatorFactoryImpl Yet it does not work. No exception is thrown during persist, and only during commit, when the entity manager is flushed, do I get an exception, and even then it is an exception from the database (because it too has a limitation on the column with size 255). So I believe that my validation is not working at all. So I'd be glad if you helped me with those two questions: 1) how to make the validation happen during persist and not during flush 2) how cause the validation to throw exceptions when validation fails?

以上就是How can I validate constrains on entities during persist of an entity in hibernate的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准