Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring Data JPA - Attributes of @Column Annotation with Example

@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.

Attributes of the @Column Annotation:

  1. name: The name of the column in the database table. Default is the property or field name.
  2. unique: Whether the column is a unique key. Default is false.
  3. nullable: Whether the database column is nullable. Default is true.
  4. insertable: Whether the column is included in SQL INSERT statements generated by the persistence provider. Default is true.
  5. updatable: Whether the column is included in SQL UPDATE statements generated by the persistence provider. Default is true.
  6. columnDefinition: The SQL fragment that is used when generating the DDL for the column.
  7. length: (Only applicable if a string-valued column is used) The maximum length of the column. Default is 255.
  8. precision: (Only applicable if a decimal column is used) The precision for a decimal (exact numeric) column.
  9. scale: (Only applicable if a decimal column is used) The scale for a decimal (exact numeric) column.

Example:

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:

  • The userName field maps to the user_name column in the database, is not nullable, and has a maximum length of 50 characters.
  • The email field is mapped to a column with the same name in the database and is configured as unique.
  • The 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.

  1. @Column annotation properties in Spring Data JPA example:

    • Here's an example demonstrating the usage of various properties of the @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...
    }
    
  2. Using length attribute in @Column annotation Spring Data JPA:

    • Use the length attribute of the @Column annotation to specify the maximum length of a column.
    // MyEntity.java
    @Column(length = 50)
    private String description;
    
  3. Nullable and unique constraints with @Column in Spring Data JPA:

    • Set the 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;
    
  4. Defining column names with @Column annotation in Spring Data JPA:

    • Specify the name attribute of the @Column annotation to define the column name in the database.
    // MyEntity.java
    @Column(name = "full_name")
    private String fullName;
    
  5. Insertable and updatable attributes in @Column Spring Data JPA:

    • Use the 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;
    
  6. Applying column definition with @Column in Spring Data JPA:

    • Use the 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;