Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: What annotation to add for default value in Spring JPA
PostPosted: Fri Sep 22, 2017 4:44 am 
Newbie

Joined: Fri Sep 22, 2017 1:50 am
Posts: 2
I have a int property , i need to add a default value via annotation. I am using Spring Data JPA(Spring Boot). I tried with below, but it didn't work. is there any other way to do this ?

Code:
@Column(name="value",columnDefinition = "int default 0")
    private int value;


Top
 Profile  
 
 Post subject: Re: How to add annotation to add default value to the Spring JPA
PostPosted: Fri Sep 22, 2017 5:06 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1582
Location: Romania
If you are using an int primitive, then the default value is always going to be 0.

If you are using hbm2ddl in production, then you are doing it all wrong. Use Flyway instead.

For numeric types, the default value is 0 if the column is NOT NULL, so just declare it as NOT NULL. If you still want to use hbm2ddl, just do it like that:

Code:
@Column(name="value", nullable = false)
private int value;

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: How to add annotation to add default value to the Spring JPA
PostPosted: Fri Sep 22, 2017 5:10 am 
Newbie

Joined: Fri Sep 22, 2017 1:50 am
Posts: 2
This is a int value and i need a give default value as 1. not the 0.


Top
 Profile  
 
 Post subject: Re: How to add annotation to add default value to the Spring JPA
PostPosted: Fri Sep 22, 2017 5:18 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1582
Location: Romania
If you wanted it to be 1, why did you defined it as 0 in the column definition?

Code:
columnDefinition = "int default 0


If you want it to be 1, then just initialize it as 1:

Code:
@Column(name="value", nullable = false)
private int value = 1;


Hibernate will use the value of 1 if you don't overwrite it. If you are using @DynamicUpdate, then you need a SQL-level DEFAULT value as well.

If you are using hbm2ddl, then the column definition should be: INT NOT NULL DEFAULT 1

Code:
@Column(name="value", nullable = false, columnDefinition="INT NOT NULL DEFAULT 1")
private int value = 1;


However, you need to make sure that the database supports the INT type too.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.