Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
In Spring Data JPA, the @Column
annotation is used to provide details on how an attribute in an entity maps to a column in the database. It is a part of the Java Persistence API (JPA) standard, not exclusive to Spring Data JPA, but Spring Data JPA often utilizes it when defining entities.
Here's a more concise breakdown of @Column
and its most commonly used attributes:
@Column
Annotation:true
.false
.INSERT
statements. Default is true
.UPDATE
statements. Default is true
.VARCHAR(255)
). Use this if you want more control over the column definition.255
.precision
is the total number of digits, and scale
is the number of digits after the decimal point.Consider an Employee
entity with some custom @Column
configurations:
@Entity public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "full_name", nullable = false) private String fullName; @Column(unique = true) private String ssn; @Column(columnDefinition = "DECIMAL(5,2)") private BigDecimal salary; // constructors, getters, setters, etc. }
In this example:
fullName
field is mapped to the full_name
column in the database and cannot be null.ssn
(Social Security Number) field is unique.salary
field is defined with a custom column definition to be a decimal column with a precision of 5 and a scale of 2.To use the @Column
annotation effectively, you'll need to understand your database's schema requirements and how you want your Java entities to map to that schema.
Using @Column annotation in Spring Data JPA:
@Column
annotation is used in Spring Data JPA to customize the properties of a column in a database table.// MyEntity.java import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class MyEntity { @Id private Long id; @Column(name = "full_name", length = 100, nullable = false, unique = true) private String fullName; // Getters and setters... }
@Column annotation properties in Spring Data JPA example:
@Column
annotation to customize column properties.// MyEntity.java import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class MyEntity { @Id private Long id; @Column(name = "description", length = 255, nullable = true, unique = false, insertable = true, updatable = true, columnDefinition = "TEXT") private String description; // Getters and setters... }
Customizing columns with @Column in Spring Data JPA:
@Column
annotation to define specific attributes such as name, length, nullable, unique, insertable, updatable, and column definition.// MyEntity.java import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class MyEntity { @Id private Long id; @Column(name = "email", length = 50, nullable = false, unique = true) private String email; // Getters and setters... }
Defining column names with @Column annotation in Spring Data JPA:
name
attribute of the @Column
annotation to define the column name in the database.// MyEntity.java import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class MyEntity { @Id private Long id; @Column(name = "full_name") private String fullName; // Getters and setters... }
Insertable and updatable attributes in @Column Spring Data JPA:
insertable
and updatable
attributes to control whether a column should be included in INSERT or UPDATE statements.// MyEntity.java import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class MyEntity { @Id private Long id; @Column(name = "created_at", insertable = false, updatable = false) private LocalDateTime createdAt; // Getters and setters... }
Nullable and unique constraints with @Column in Spring Data JPA:
nullable
attribute to false
to enforce a NOT NULL constraint. Use the unique
attribute to enforce a UNIQUE constraint.// MyEntity.java import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class MyEntity { @Id private Long id; @Column(nullable = false, unique = true) private String username; // Getters and setters... }
Applying column definition with @Column in Spring Data JPA:
columnDefinition
attribute to provide a custom SQL fragment that defines the data type of the column.// MyEntity.java import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class MyEntity { @Id private Long id; @Column(name = "status", columnDefinition = "VARCHAR(10) DEFAULT 'ACTIVE'") private String status; // Getters and setters... }