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

2
  • 1
    Why not use a boolean instead of Boolean... Commented Nov 14, 2014 at 12:40
  • 1
    Presumably he might need his Boolean to be null if he has a legacy schema that allows null on that column. Commented Nov 14, 2014 at 13:57

2 Answers 2

1

You can just do fallow in your class definition:

public YourClass{
....
private Boolean isValid=false;
....
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried, but it is not working :(
How do you initialize it ? Did you load it from database? Maybe you just have true in your database ?
0

Just add default value with column

@Column(name="isValid",columnDefinition="tinyint(1) default '0'")
private Boolean isValid;

4 Comments

I get compilation error "create annotation Column", could you please mention the required jar. I am not using Hibernate ORM.
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 This is the only way to set default value using java JPA annotations. check edited answer and try again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.