0

I am a newbie and I am working with java bean validations.

I have a java bean class as given below with a null and size constraint.

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Employee {
@NotNull
private int id;  
@NotNull
@Size(min=4, max=9)
private String name;  
public Employee(){}       
public void setId(int id){this.id=id;}  
public int getId(){return id;}  
public void setName(String name){this.name=name;}  
public String getName(){return name;}     
}

and I have a main class as below

public class Test {

public static void main(String[] args) {
    Employee e=new Employee();
      e.setId(0);
      System.out.println(e.getId());
    e.setName("abc");  

    System.out.println(e.getName()); 
    }}

This is expected to throw error when I pass name with length less than 4. But this class is executing successfully for all the values I pass. Let me know what I am missing here.

Thanks in advance.

user001
  • 3
  • 3
  • this might help http://stackoverflow.com/questions/8756768/annotations-from-javax-validation-constraints-not-working – smoggers Jul 23 '15 at 09:27
  • possible duplication of http://stackoverflow.com/questions/8756768/annotations-from-javax-validation-constraints-not-working – Spartan Jul 23 '15 at 10:58

1 Answers1

0

Example code

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Employee {
@NotNull
private int id;  
@NotNull
@Size(min=4, max=9, message="length error")
private String name;  
public Employee(){}       
public void setId(int id){this.id=id;}  
public int getId(){return id;}  
public void setName(String name){this.name=name;}  
public String getName(){return name;}   

main class:

public class Test {
    public static void main(String[] args) {
        Employee e = new Employee();
        e.setId(0);
        e.setName("abc");
        ValidatorFactory v = Validation.buildDefaultValidatorFactory();
        Validator validator = v.getValidator();
        Set<ConstraintViolation<Employee>> set = validator.validate(e);
        for (ConstraintViolation<Employee> constraintViolation : set) {
            System.out.println(constraintViolation.getMessage());
        }
    }
}
Beniamin
  • 56
  • 2
  • Provide more context. Don't throw source code into the answer and leave it without explanation and comments. – Manuel Jul 23 '15 at 09:43
  • Thanks Beniamin, when i run the above code i get and error - "Exception in thread "main" javax.validation.ValidationException: Unable to find a default provider at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264) at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)" ... do I need to add any dependencies? – user001 Jul 23 '15 at 09:56
  • I am sorry,but i don't get your error.Did you import the library or the library conflict?Please provide more information,thanks! – Beniamin Jul 24 '15 at 08:32
  • I have not imported any libraries. i just did Run as JAVA application on Test class. I guess i need to add some jars to the class path. but i am not getting which jars should be added. Can i do the bean validations in java without having any frameworks? – user001 Jul 27 '15 at 16:02
  • It does need the "javaEE 6.0" Library.It is not a framework, but the Java comes with the class library.If you want do it without that,I am so sorry I can not give you more help. – Beniamin Jul 28 '15 at 07:54
  • Hi Beniamin, I meant to say i want to do the validations in Core Java without frameworks like hibernate,maven,etc. I am having jdk 1,7, and when i run the code i get error - Unable to find a default provider at javax.validation.Validation$GenericBootstrapImpl. – user001 Jul 28 '15 at 10:39
  • It depends a jar named "bean-validator.jar",If you use the default library,there should be no.Check your library. – Beniamin Jul 30 '15 at 02:18