Tulshiram Pawde
Tulshiram Pawde

Reputation: 184

Set default values using hibernate annotations in MYSQL

I have searched on internet and seen ideas about setting default values in entity class using hibernate annotations in mysql and I have done with setting default values to column with datatype integer only as follows.

@Column(name = "COLUMN_NAME", insertable=false, updatable = false, nullable = false, columnDefinition = "int default 1")
protected Integer test_id;

and is working fine.

But I haven't seen any example for varchar/string datatype and I have try with different types in columnDefinition like,

columnDefinition = "TEXT default `TEST`"  
columnDefinition = "TEXT default TEST"  
columnDefinition = "varchar(255) default `15-JUL-1980`")  
columnDefinition = "varchar(255) default 15-JUL-1980")  
columnDefinition = "varchar default 15-JUL-1980")  
columnDefinition = "varchar default `15-JUL-1980`")  
columnDefinition = "CHAR(100) default `15JUL1980`")  
columnDefinition = "CHAR default `15JUL1980`")

With length attribute:-

`length=100, columnDefinition = "CHAR default `15JUL1980`")` 

etc.

but in this case table is not created by hibernate, that may be due to wrong syntax. But if I am creating the table with only integer values as above table is created.

Upvotes: 4

Views: 47820

Answers (3)

Niravdas
Niravdas

Reputation: 476

simply set default value in your Java code, just initialize your variable like this - private int myColumn = 100; and String as

@Column(name="order_status" , length=10 )
private String orderStatus = "Pending";

it's work for me !! i know it's too late but i hope this will help someone !!

Upvotes: 0

Inhan
Inhan

Reputation: 526

columnDefinition is works, but it is database dependent.
and there are many other solution(Using @PrePersist annotation, modifying 'getters'), but I recommend you set default value in DBMS, then use @DynamicInsert/@DynamicUpdate annotations in model class.

See example below(Spring data JPA + Hibernate).

// Model class
@Entity
public class Person {
    private Integer age;
    private String name;
    private String phone;
    private String address;

    // setters and getters
}

// Insert method in service class
@Override
@Transactional
public Person create() {
    Person createdPerson = new Person();

    createdPerson.setAge(20);
    createdPerson.setName("Foo");
    //createdPerson.setPhone("Unknown");
    //createdPerson.setAddress("Somewhere");

    return personRepository.save(createdPerson);
}

Hibernate will generate following insert SQL statement.

INSERT INTO person (age, name, phone, address)
VALUES (?, ?, ?, ?)

you can see Hibernate insert unnecessary columns(phone, address), too.

but, after add @DynamicInsert annotation to Model class like below,

@Entity
@DynamicInsert
public class Person {
    ...
}

you can see Hibernate insert only necessary columns.

INSERT INTO person (age, name)
VALUES (?, ?)

and uninitilized columns(phone, address) set their value by DBMS default value.

Upvotes: 0

Tulshiram Pawde
Tulshiram Pawde

Reputation: 184

Below line of code is working fine

@Column(name = "cname", insertable=false, updatable = false, nullable = false, columnDefinition = "varchar(255) default '15-JUL-1980'") 

Upvotes: 13

Related Questions