Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
@Column
is an annotation provided by the Java Persistence API (JPA). It is used to specify properties or constraints on entity bean fields or properties. When using Spring Data JPA, entities often use this annotation to customize the column mappings between the Java class and the database table.
@Column
Annotation:false
.true
.INSERT
statements generated by the persistence provider. Default is true
.UPDATE
statements generated by the persistence provider. Default is true
.255
.Consider an entity User
which needs some specific column configurations:
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "user_name", nullable = false, length = 50) private String userName; @Column(unique = true) private String email; @Column(columnDefinition = "TEXT") private String bio; // Getters, setters, and other methods... }
In this example:
userName
field maps to the user_name
column in the database, is not nullable, and has a maximum length of 50 characters.email
field is mapped to a column with the same name in the database and is configured as unique.bio
field uses the columnDefinition
attribute to specify that the corresponding column in the database should be of type TEXT
.Note: The behavior and availability of some attributes might vary depending on the underlying database and JPA provider (like Hibernate). Always refer to the JPA documentation and your specific JPA provider's documentation for any nuances.
@Column annotation properties in Spring Data JPA example:
@Column
annotation.// 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, insertable = true, updatable = false, columnDefinition = "VARCHAR(100)") private String fullName; // Getters and setters... }
Using length attribute in @Column annotation Spring Data JPA:
length
attribute of the @Column
annotation to specify the maximum length of a column.// MyEntity.java @Column(length = 50) private String description;
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 @Column(nullable = false, unique = true) private String username;
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 @Column(name = "full_name") private String fullName;
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 @Column(insertable = false, updatable = false) private String createdBy;
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 @Column(columnDefinition = "VARCHAR(255) DEFAULT 'UNKNOWN'") private String status;