0

I want to set default values if not specified by user. eg, for Boolean private isValid , if we don't specify any json value, then it should set false . I am using hibernate validator, @NotNull, @NotBlank, @Size, @Range...Is there any annotations to set default value for boolean, int, float, irrespective of data types. I tired hard, but did not come up with any solution. Still I am trying..Your help is highly appreciated ! Thank you

Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
sayan
  • 501
  • 2
  • 7
  • 21

2 Answers2

1

You can just do fallow in your class definition:

public YourClass{
....
private Boolean isValid=false;
....
}
Maksym
  • 4,434
  • 4
  • 27
  • 46
0

Just add default value with column

@Column(name="isValid",columnDefinition="tinyint(1) default '0'")
private Boolean isValid;
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
  • I get compilation error "create annotation Column", could you please mention the required jar. I am not using Hibernate ORM. – sayan Nov 14 '14 at 12:32
  • @sayan Get jar here http://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api/1.0.1.Final – Harshal Patil Nov 15 '14 at 08:59
  • I tried, but it didnt work. I am not using Hibernate ORM for my code, that's why it didnt work, I guess. I am only using hibernate-validator jars for validation purpose, not hibernate orm. Is there anything to set default values using annotations ? Please help. thnx. – sayan Nov 17 '14 at 06:07
  • @sayan This is the only way to set default value using java JPA annotations. check edited answer and try again. – Harshal Patil Nov 17 '14 at 06:28