Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

entity Boolean field default value

 
Alexander Curvers
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whats the best way to specify a defailt value for a Boolean entity field,
i would like it to be false but now i get null when i use the getter
even though i set it false in the constructor of the entity it gets set to null when the entity is loaded from database (in the database the value is null by default)
i would like to use annotations so that the table and column are generated in the DB with a default value for the column..
 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, JPA does not provide an explicit way to set default values. Use Hibernate annotations for specific defaults.
Still you can do the same thing using @Column and @PrePersist.
- nullable to set column to not allow null values
- columnDefinition - to give a default value (pay attention, this is an sql specific vendor syntax)

If you want to be independent of db, try @PrePersist to set default value like here:


Hope it helped![/img]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic