bean-validation

Annotations from javax.validation.constraints not working

血红的双手。 提交于 2019-11-27 00:36:57
What configuration is needed to use annotations from javax.validation.constraints like @Size , @NotNull , etc.? Here's my code: import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class Person { @NotNull private String id; @Size(max = 3) private String name; private int age; public Person(String id, String name, int age) { this.id = id; this.name = name; this.age = age; } } When I try to use it in another class, validation doesn't work (i.e. the object is created without error): Person P = new Person(null, "Richard3", 8229)); Why doesn't this apply

Custom Class Level Constraint for Crossfield validation not called

邮差的信 提交于 2019-11-26 23:17:19
问题 I am trying to implement a crossfield validation (JSR-303) with a custom annotation on class level. However the isValid method is not called (but the initialize method). So my question is: Why is the isValid method not being called for this class level validator? Defining it on property level works! I tried it on JBoss AS 7 and Websphere AS 8. Here is the code and a JUnit test (which works) Test.java public class Test { @org.junit.Test public void test() throws ParseException { Person person

can I override a jsr-303 validation annotation

僤鯓⒐⒋嵵緔 提交于 2019-11-26 23:15:37
问题 I have a test like below: public class TestSizeAnnotation { public static void main(String[] args) { System.out.println( Validation.buildDefaultValidatorFactory().getValidator().validate(new C())); } public static class P { private List<String> lst = newArrayList("AA"); @Size(max=0, message="P") public List<String> getLst() { return lst; } public void setLst(List<String> lst) { this.lst = lst; } } public static class C extends P { @Override @Size(max=5, message="C") public List<String> getLst

JSR303 custom validators being called twice

删除回忆录丶 提交于 2019-11-26 23:10:29
问题 I am creating a website using Spring MVC and for persistence I am using Spring Data JPA with Hibernate 4 as my JPA provider. Validation is being handled at present with Hibernate Validator. I have a problem whereby my validators are being called twice and I can't figure out why. The main reason this is a problem is because the second time round, dependencies are not being autowired into the validator and I am getting a null pointer exception. The following is the sequence of calls leading up

JSR-303 Type Checking Before Binding

纵饮孤独 提交于 2019-11-26 22:52:52
问题 model.... @Digits(integer=5, fraction=0, message="The value must be numeric and less than five digits") private int value; beans file.... <mvc:annotation-driven /> controller.... @RequestMapping(value = "/admin/save.htm", method = { RequestMethod.POST }) public ModelAndView saveSection(@Valid @ModelAttribute Section section, BindingResult result) { if(result.hasErrors()) { return new ModelAndView("admin/editSection", "section", section); } How do I restrict "value" to just numerics? If I

Hibernate - Error activating Bean Validation integration

混江龙づ霸主 提交于 2019-11-26 22:02:54
问题 I am trying to set up Hibernate. But when i try to create my Session Factory, with this code: Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); I get the error: org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration at org.hibernate.cfg

Using Java EE 6 Bean Validation

安稳与你 提交于 2019-11-26 21:44:08
问题 I am trying to use Java EE 6 Validation as specified here http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html I have annotated a simple field @Max(11) @Min(3) private int numAllowed; The docs says "For a built-in constraint, a default implementation is available" but how do I specify this. My constraints checks are not kicking in. I would expect it to work on calling of the setter method for the field. The only import in my class is import javax.validation.constraints.Max; import javax

Unexpected UnsupportedOperationException on Hibernate validation failure

折月煮酒 提交于 2019-11-26 21:43:02
问题 Why am I getting an UnsupportedOperationException from Hibernate upon validation failure? I'm hoping for a ConstraintViolationException. Below is my DAO. When it passes validation it works fine. I'm using Hibernate and Hibernate Validator 4. I'm using Websphere 8.0. I think WebSphereExtendedJtaPlatform is the culprit, but I have no idea why. If I look at what's supposedly the source code for WebSphereExtendedJtaPlatform (follow link for the source) it looks like most of the methods there just

JSR-303 validation with custom message

混江龙づ霸主 提交于 2019-11-26 21:24:51
问题 I'm using Spring 3.0.5-RELEASE, with JSR-303 style validation and Hibernate validator 4.1.0-Final. My model class looks something like this: public class Model { @Max(value=10,message="give a lower value") Integer n; } And this is passed as request param in a spring mvc servlet with binding. So the request would look something like this: http://localhost:8080/path?n=10 What I want is to be able to customize the error message when there is a type mismatch exception, e.g. http://localhost:8080

Autowired Repository is Null in Custom Constraint Validator

心已入冬 提交于 2019-11-26 21:02:04
问题 I have a Spring based webapp. I am using several repository classes with annotations @Repository, @Transactional in my Controllers. That part works fine. I created a Custom Constraint Validator which has to access the repository. Now I am not able to understand why the Repository is null. I tried annotating the validator with @Component annotation. My base package containing all these classes is in the part of the xml. So what else should I do to ensure the repository dependency injection