liveBook · Manning

@Column annotation

As explained in Java Persistence with Spring Data and Hibernate epub

The JPA @Column annotation specifies how a persistent property or field maps to a relational database column.

The JPA @Column annotation customizes how a persistent field or property maps to a relational column. It can override the column name and declare nullability, precision, scale, or length. section 6.1.1 section 6.3.1

Name a required price column

@Column(name = "START_PRICE", nullable = false) BigDecimal initialPrice; This maps initialPrice to START_PRICE and declares the value required. The provider checks for null when saving, and schema generation produces a NOT NULL constraint. Without the required-value mapping, a database constraint can instead reject a null SQL value. section 6.1.1

@Basic(optional = false), @Column(nullable = false), and Bean Validation @NotNull have the same effect on provider null checking and generated NOT NULL constraints. Prefer @NotNull when manual validation or automatic presentation-layer validation is also useful. section 6.1.1

For schema generation, precision and scale override the default NUMERIC(19, 2) mapping of a BigDecimal property. String length influences Hibernate’s SQL type selection along with the configured dialect. The default String length is 255; for MySQL, larger declared lengths can select MEDIUMTEXT or LONGTEXT instead of VARCHAR. section 6.3.1

@Column can also name the value column of an element collection. In a set of image filenames, @ElementCollection maps the values, @CollectionTable names the IMAGE table and its join column, and @Column names the FILENAME column. section 8.1.4

Listing 8.1 Images mapped as a simple set of strings
Path: Ch08/mapping-collections/src/main/java/com/manning/javapersistence
 /ch08/setofstrings/Item.java
 
@Entity
public class Item {
    // . . .
 
    @ElementCollection                                      #1
    @CollectionTable(
            name = "IMAGE",                                 #2
            joinColumns = @JoinColumn(name = "ITEM_ID"))    #3
    @Column(name = "FILENAME")                              #4
    private Set<String> images = new HashSet<>();           #5
#1 Declare the <kbd>images</kbd> field as an <kbd>@ElementCollection</kbd>. We refer here to image paths on the system, but for brevity we’ll use the names of the fields and columns, such as <kbd>image</kbd> or <kbd>images</kbd>.
#2 The collection table will be named <kbd>IMAGE</kbd>. Otherwise it would default to <kbd>ITEM_ IMAGES</kbd>.
#3 The join column between the <kbd>ITEM</kbd> and the <kbd>IMAGE</kbd> tables will be <kbd>ITEM_ID</kbd> (the default name, in fact).
#4 The name of the column that will contain the string information from the <kbd>images</kbd> collection will be <kbd>FILENAME</kbd>. Otherwise it would default to <kbd>IMAGES</kbd>.
#5 Initialize the <kbd>images</kbd> collection as a <kbd>HashSet</kbd>.

See the complete annotation combination for a set of image filenames. section 8.1.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains naming, nullability, precision, scale, and length, while deferring rarely needed SQL-level parameters.
Examples
Uses a required initial price and a set of image filenames to illustrate column mappings.
What you can do after
Apply it via JPA and Hibernate to name columns and configure constraints or generated SQL types.
How it connects
Connects column mapping with Bean Validation, schema generation, SQL dialects, and element collections.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.