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
