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.

Use JPA’s @Column when a persistent field needs a column mapping other than the default. It can set the column name, declare whether null is allowed, and supply details used during schema generation. section 6.1.1 section 6.3.1

Require a price and rename its column

@Column(name = "START_PRICE", nullable = false) BigDecimal initialPrice; The field maps to START_PRICE rather than its default column name. A null value is checked before an INSERT or UPDATE, and a generated schema includes a NOT NULL constraint. section 6.1.1

For required values, @Basic(optional = false) and Bean Validation’s @NotNull provide the same JPA-side null check and generated NOT NULL constraint. Prefer @NotNull when the application also needs manual or presentation-layer validation. section 6.1.1

SQL type choices still depend on Hibernate’s configured dialect. @Column can set precision and scale for a BigDecimal column or length for a String column; Hibernate uses those settings when generating the schema. Without a length override, its String default is 255. section 6.3.1

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

For an @ElementCollection of image filenames, @Column(name = "FILENAME") names the element-value column in the IMAGE collection table; @CollectionTable and @JoinColumn configure the table and its link to the item. section 8.1.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Details name, nullability, precision, scale, and length overrides, while leaving catalog and schema parameters largely aside.
Examples
Shows a price field mapped to START_PRICE and a set of image filenames mapped to FILENAME in IMAGE.
What it assumes
Assumes familiarity with Java fields and database columns while demonstrating the mapping annotations directly.
What you can do after
Apply it via JPA/Hibernate to set column names, nullability, numeric precision and scale, or string length.
How it connects
Connects @Basic and Bean Validation @NotNull to nullability, and @ElementCollection and @CollectionTable to collection columns.

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.