Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring Data JPA - @Column Annotation

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:

Attributes of the @Column Annotation:

  1. name: Represents the name of the column in the database. By default, it's the same as the name of the field in the entity.
  2. nullable: Indicates if the column can have null values. The default is true.
  3. unique: Indicates if the values in the column should be unique. The default is false.
  4. insertable: Specifies if the column value should be included in SQL INSERT statements. Default is true.
  5. updatable: Specifies if the column value should be included in SQL UPDATE statements. Default is true.
  6. columnDefinition: Specifies the SQL fragment for creating the column in the database (e.g., VARCHAR(255)). Use this if you want more control over the column definition.
  7. length: If the column is a string type, this specifies the column's maximum length. Default is 255.
  8. precision and scale: Used for decimal columns. precision is the total number of digits, and scale is the number of digits after the decimal point.

Example:

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:

  • The fullName field is mapped to the full_name column in the database and cannot be null.
  • The ssn (Social Security Number) field is unique.
  • The 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.

  1. Using @Column annotation in Spring Data JPA:

    • The @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...
    }
    
  2. @Column annotation properties in Spring Data JPA example:

    • Here's an example demonstrating the use of various properties of the @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...
    }
    
  3. Customizing columns with @Column in Spring Data JPA:

    • Customize column properties using the @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...
    }
    
  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
    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...
    }
    
  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
    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...
    }
    
  6. 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
    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...
    }
    
  7. 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
    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...
    }