Spring Data JPA - Attributes of @Column Annotation with Example - GeeksforGeeks

29 Dec, 2021
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. 
@Column(length=”varchar(255)/Integer 23′′): Adding the column size of the particular column in the MySQL table@Column(columnDefinition = “varchar(22) default ‘Aayush'”): Adding the default value in the column@Column(nullable=false): Adding the not-null constraint to the column of the table
@Column(length=”varchar(255)/Integer 23′′): Adding the column size of the particular column in the MySQL table
@Column(columnDefinition = “varchar(22) default ‘Aayush'”): Adding the default value in the column
@Column(nullable=false): Adding the not-null constraint to the column of the table
Let’s discuss each operation one by one. 
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:
@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:
@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:
@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:
Java-Spring-Data-JPA
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Constructors in Java
Stream In Java
Exceptions in Java
StringBuilder Class in Java with Examples
Comparator Interface in Java with Examples
Generics in Java
Functional Interfaces in Java
Java Programming Examples
HashMap get() Method in Java