Home Hibernate Hibernate/JPA - Default Column Values

Hibernate/JPA – Default Column Values

In this tutorial, we are going to see how to set default column values to entity properties and in schema generation in JPA and Hibernate applications.

1. Setting Default values in Entity

In JPA or Hibernate, we can set default column values to entity properties directly. Let’s see an example.

@Entity
public class Student {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
	@GenericGenerator(name = "native", strategy = "native")
	@Column(name = "ID")
	private Long studentId;

	@Column(name = "FNAME")
	private String firstName = "Alexander";

	@Column(name = "LNAME")
	private String lastName = "Fleming";

        @Column(name = "CONTACT_NO")	
	private String contactNo = "XX-XXX-XXX-XXXX";
	
	@Column(name= "STATUS")
	private Boolean active = true;

	// setters, gettres, toString() - omitted

}

Now, every time when you create new Student instance, default column values automatically set to properties. Let’s test that.

public static void main(String[] args) {
     ...
     Student student = new Student();
     entityManager.persist(student);
     ...
     Student s = entityManager.find(Student.class, student.getStudentId());
     System.out.println(s)
     ...
}

Output Results:

...
Hibernate: 
    insert 
    into
        STUDENT
        (STATUS, CONTACT_NO, FNAME, LNAME) 
    values
        (?, ?, ?, ?)
Student [studentId=5, firstName=Alexander, lastName=Fleming, contactNo=XX-XXX-XXX-XXXX, active=true]
...

2. Setting Default column values while Schema generation

2.1. Default column values in JPA

JPA allows to generate Schema definitions when you set hibernate.hbm2ddl.auto value to create or create-drop or update. If you would need to set columns defaults values in SQL tables directly, we can use JPA @Column annotation with columnDefinition attribute.

For testing, set hibernate.hbm2ddl.auto to create in configuration and then update entity like following example.

@Entity
public class Student {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
	@GenericGenerator(name = "native", strategy = "native")
	@Column(name = "ID")
	private Long studentId;

	@Column(name = "FNAME", columnDefinition = "varchar(50) default 'Alexander'")
	private String firstName;

	@Column(name = "LNAME", columnDefinition = "varchar(50) default 'Fleming'")
	private String lastName;

        @Column(name = "CONTACT_NO", columnDefinition = "varchar(15) default 'XX-XXX-XXX-XXXX'")	
	private String contactNo;
	
	@Column(name= "STATUS", columnDefinition = "tinyint(1) default true")
	private Boolean active = true;

	// setters, gettres, toString() - omitted

}

For testing, just initialize EntityManager instance.

public static void main(String[] args) {
        ...			
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("jbd-pu");			
        EntityManager entityManager = emf.createEntityManager();
        ...
}

Output Results:

...
Hibernate: 
    
    create table STUDENT (
       ID bigint not null auto_increment,
        STATUS tinyint(1) default true,
        CONTACT_NO varchar(15) default 'XX-XXX-XXX-XXXX',
        FNAME varchar(50) default 'Alexander',
        LNAME varchar(50) default 'Fleming',
        primary key (ID)
    ) engine=InnoDB
...

2.2. Default column values in Hibernate

In Hibernate, @ColumnDefault annotation can be used to identify the DEFAULT value to apply to the associated column via DDL. Let’s see an example.

@Entity
public class Student {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
	@GenericGenerator(name = "native", strategy = "native")
	@Column(name = "ID")
	private Long studentId;

	@ColumnDefault("'Alexander'")
	@Column(name = "FNAME")
	private String firstName;

	@ColumnDefault("'Fleming'")
	@Column(name = "LNAME")
	private String lastName;

	@ColumnDefault("'XX-XXX-XXX-XXXX'")
       @Column(name = "CONTACT_NO")	
	private String contactNo;
	
	@ColumnDefault("true")
	@Column(name= "STATUS")
	private Boolean active = true;

	// setters, gettres, toString() - omitted

}

For testing, create HibernateSession. You will see following reults.

...

Hibernate: 
    
    create table STUDENT (
       ID bigint not null auto_increment,
        STATUS bit default true,
        CONTACT_NO varchar(255) default 'XX-XXX-XXX-XXXX',
        FNAME varchar(255) default 'Alexander',
        LNAME varchar(255) default 'Fleming',
        primary key (ID)
    ) engine=InnoDB
...

3. Conclusion

In this tutorial, we have learned how to set default column values using @Column annotation columnDefinition attribute and @ColumnDefault annotation while creating entity and in Schama definition in JPA and Hibernate.

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Subscribe

Subscribe for latest updates.

 

Loading

Categories