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
Asked
Active
Viewed 1.1k times
0

Harshal Patil
- 6,659
- 8
- 41
- 57

sayan
- 501
- 2
- 7
- 21
-
1Why not use a `boolean` instead of `Boolean`... – M. Deinum Nov 14 '14 at 12:40
-
1Presumably he might need his Boolean to be null if he has a legacy schema that allows null on that column. – Adam Gent Nov 14 '14 at 13:57
2 Answers
1
You can just do fallow in your class definition:
public YourClass{
....
private Boolean isValid=false;
....
}

Maksym
- 4,434
- 4
- 27
- 46
-
-
How do you initialize it ? Did you load it from database? Maybe you just have true in your database ? – Maksym Nov 14 '14 at 12:47
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