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
