• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

entity Boolean field default value

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic