Open In App

Spring Data JPA – Attributes of @Column Annotation with Example

Last Updated : 29 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Spring Boot is built on the top of the spring and contains all the features of spring. Spring also provides JPA and hibernate to increase the data manipulation efficiency between the spring application and the database. In very simple terms we can say JPA (Java persistence API) is like an interface and the hibernate is the implementation of the methods of the interface Like how insertion will be down is already defined with the help of hibernate. In this article, we will discuss the following most important and most used attributes of @Column annotation with examples. 

  1. @Column(length=”varchar(255)/Integer 23″): Adding the column size of the particular column in the MySQL table
  2. @Column(columnDefinition = “varchar(22) default ‘Aayush'”): Adding the default value in the column
  3. @Column(nullable=false): Adding the not-null constraint to the column of the table

Let’s discuss each operation one by one. 

Example

Step 1: Go to this link. Fill in the details as per the requirements. For this application:

Project: Maven
Language: Java
Spring Boot: 2.5.6
Packaging: JAR
Java: 11
Dependencies: Spring Web,Spring Data JPA, MySql Driver

Click on Generate which will download the starter project.

Step 2: Extract the zip file. Now open a suitable IDE and then go to File > New > Project from existing sources > Mapping and select pom.xml. Click on import changes on prompt and wait for the project to sync as pictorially depicted below as follows:

Step 3: Adding the necessary properties in the application.properties file. (mapping is the database name)

spring.datasource.username=root
spring.datasource.password=Aayush
spring.datasource.url=jdbc:mysql://localhost:3306/mapping
spring.jpa.hibernate.ddl-auto=update

Step 4: Create a model folder in the project folder and make a StudentInformation class.

Project structure:

1. Adding the size of the column 

@Column(length=”varchar(255)/Integer 23″) annotation is used for adding the column size of MySQL column using SpringBoot

StudentInformation.java

Java




@Entity
@Table(name = "Student")
public class StudentInformation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int rollno;
  
      @Column(name = "Student_name" ,length = 30)
      private String name;
  
    public int getRollno() { return rollno; }
  
    public StudentInformation() {}
  
    public StudentInformation(int rollno, String name)
    {
        this.rollno = rollno;
        this.name = name;
    }
  
    public void setRollno(int rollno)
    {
  
        this.rollno = rollno;
    }
  
    public String getName() { return name; }
  
    public void setName(String name) { this.name = name; }
}


Run the main application:

Database output:

2. Adding the default value in the column

@Column(columnDefinition = “varchar(22) default ‘Aayush'”)

StudentInformation.java

Java




@Entity
@Table(name = "Student")
public class StudentInformation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int rollno;
  
      @Column(columnDefinition = "varchar(22) default 'Aayush'")
      private String name;
  
    public int getRollno() { return rollno; }
  
    public StudentInformation() {}
  
    public StudentInformation(int rollno, String name)
    {
        this.rollno = rollno;
        this.name = name;
    }
  
    public void setRollno(int rollno)
    {
  
        this.rollno = rollno;
    }
  
    public String getName() { return name; }
  
    public void setName(String name) { this.name = name; }
}


Run the main application:

Database output:

3. Adding a not-null constraint to the column

@Column(nullable=false) annotation is used for adding the not null constraint on the particular column of the table.

StudentInformation.java

Java




@Entity
@Table(name = "Student")
public class StudentInformation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int rollno;
  
      @Column(nullable=false)
      private String name;
  
    public int getRollno() { return rollno; }
  
    public StudentInformation() {}
  
    public StudentInformation(int rollno, String name)
    {
        this.rollno = rollno;
        this.name = name;
    }
  
    public void setRollno(int rollno)
    {
  
        this.rollno = rollno;
    }
  
    public String getName() { return name; }
  
    public void setName(String name) { this.name = name; }
}


Run the main application:

Terminal Output:

Database Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads