19

I have a boolean property in my entity. Here's my annotations for it:

@Column(name = "IS_ACTIVE", nullable = false, columnDefinition="BIT DEFAULT 1", length = 1)
public Boolean getActive() {
    return isActive;
}

But columnDefinition="BIT DEFAULT 1" doen't work perfectly. Here's SQL code I get as result for generated table:

IS_ACTIVE BIT(1) NOT NULL,

What am I doing wrong?

And so when I try to save an instance of this class to the database I get the exception:

`com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'IS_ACTIVE' cannot be null`

If I remove nullable = false property:

@Column(name = "IS_ACTIVE", columnDefinition="BIT DEFAULT 1", length = 1)
public Boolean getActive() {
    return isActive;
}

so I can save a created object in this case. But it's still the default value is not set and I get NULL in the value of this field in database.

Any ideas please? I use MySQL Server 5.1 if it's important. I would be very grateful for any help. Thanks in advance!

Philiph Bruno
  • 413
  • 2
  • 4
  • 8

3 Answers3

38

Try using BOOLEAN data type, define your @Column annotation like that:

@Column(name = "IS_ACTIVE", columnDefinition = "boolean default true", nullable = false)
private Boolean active = true;
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • Unfortunately it didn't help. Only BIT type has changed to TINYINT in database. But all the same it's not received the default value. Thanks for your suggestion! – Philiph Bruno Nov 27 '12 at 22:20
  • Yes, of course. I still use **create-drop** of `hibernate.hbm2ddl.auto` in test mode. And now I have `IS_ACTIVE TINYINT(1) NOT NULL DEFAULT '0',` in database. It's very well! But why is default value NULL? Do you have any ideas? – Philiph Bruno Nov 27 '12 at 22:35
  • 1
    And I again get the exception `com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'IS_ACTIVE' cannot be null` when I am trying to save an instance of this class... – Philiph Bruno Nov 27 '12 at 22:39
  • Try to initialize boolean value in class as well. Updated my asnwer. – Aleksandr M Nov 27 '12 at 22:41
  • Or change Boolean to primitive boolean. – Aleksandr M Nov 27 '12 at 22:44
  • It's work properly. Thank you! Is this approach good practice? (initialize a variable in the Java code and not in the database) Or is this issue cannot be solved by different way? – Philiph Bruno Nov 27 '12 at 22:49
  • If you want to set default to some entity variable you need to do it in Java code and in database. Then you need not worry about how do you put your object to database by executing SQL `insert` or persisting entity with ORM. – Aleksandr M Nov 28 '12 at 09:51
  • @AleksandrM but wouldn't that create a maintenance overhead, it will be maintained in both db and java code ? – Osama.070032 Mar 03 '20 at 14:17
2

reference to the correct answer in this link How to set default value in Hibernate

If you want a real database default value, use columnDefinition - @Column(name = “myColumn”, nullable = false, columnDefinition = “int default 100"). Notice that the string in columnDefinition is database dependent. Also if you choose this option, you have to use dynamic-insert, so Hibernate doesn't include columns with null values on insert. Otherwise talking about default is irrelevant.

The key of the solution is dynamic-insert annotation. You can add it to your entity class as this example: @org.hibernate.annotations.Entity(dynamicInsert = true)

Community
  • 1
  • 1
Peter T.
  • 8,757
  • 3
  • 34
  • 32
0

Verify that the default value of false is set in your entity class for the boolean attribute. Ensure that you have something like this:

private boolean yourBooleanAttribute = false;

You can explicitly specify the column definition using the @Column annotation in your entity class. Make sure you specify the columnDefinition attribute to set the default value to false in the generated SQL:

@Column(columnDefinition = "boolean default false") private boolean yourBooleanAttribute;

MLone
  • 1
  • 1