0

I have a basic SpringBoot/Hibernate application that deals with a database. I have a column in an Oracle table that represents a boolean value. At the database level, it is represented this way - P_IND CHAR(1 BYTE) DEFAULT 'N' NOT NULL

At the entity level, I am mapping it this way -

@Column(name = "P_IND")
@Type(type = "yes_no")
private boolean ind;

When creating new records, the default needed to be an 'N' and this worked as expected. Now the default needs to be true ('Y' in the database). I could do it this way

a) private boolean ind = true;

or add this annotation

b) @ColumnDefault("true")

  1. Isn't a) more advantageous in that you could create/persist entities with both true/false values?
  2. Would b) allow you to create both (ind is a primitive boolean) and if so, is the annotation a better way of doing it?
spring5150
  • 65
  • 1
  • 9
  • These two options are not interchangeable - you need both – Andrey B. Panfilov Jul 09 '22 at 18:52
  • You will need b) if records may be inserted from other processes, because it will create the column with default in DDL. – PeterMmm Jul 09 '22 at 18:52
  • Does this answer your question? [Setting default values for columns in JPA](https://stackoverflow.com/questions/197045/setting-default-values-for-columns-in-jpa) – Eugene Jul 09 '22 at 22:54
  • You do not need both an initial value and defaults in the database; Doing both is duplicated overhead, as having the 'boolean' set to true forces providers to set it to true in the insert anyway; which is required to keep your object in synch with the database data. An initial value (in the DB) ties application business logic into the database, and business logic needs can change over time - adding unnecessary risking to existing apps. Don't do DB defaults unless you have a strong need for them (like a legacy JDBC application) – Chris Jul 11 '22 at 15:34

1 Answers1

1

They do two different things.

@ColumnDefault is an Hibernate ORM specific annotation (not JPA). It identifies the default value that Hibernate will use in the column definition when it's generating the schema at start up. It means that's only used when Hibernate creates the column.

private boolean ind = true;

Using a primitive type means that the value will always be true or false (I think in Java the default is false) during the persist or updates. So the @ColumnDefault doesn't really matter (unless you run custom insert queries without Hibernate ORM).

If you change the type to Boolean, then ind can be null. This means that the default value used on insertion will be the one used when the column was created. If you have created the database using Hibernate ORM, it's the value in @ColumnDefault.

Anyway, it seems you need both.

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • ```private boolean ind = true;``` works as intended, without the ```ColumnDefault``` annotation. ```@ColumnDefault("true")``` ```private boolean ind;``` still creates a record with a ```false``` ('N' in the database). So is there something that I am missing? – spring5150 Jul 11 '22 at 14:52
  • 1
    You didn't miss anything. If the value is false in the boolean, your JPA provider will add the value for false to the insert statement; the DB column's default value only comes into play if it is left null. So you would have to use a Boolean and leave it null to use the DB default, which means you won't know in your Entity what value it is set to until you read it back (refresh or reload from the DB). – Chris Jul 11 '22 at 15:44
  • I've updated my answer, hopefully it's clearer now. – Davide D'Alto Jul 11 '22 at 15:58