From a2fcecfe8ccaa3809743a92eceb231eb7e7ce8fd Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Sat, 26 Sep 2020 11:45:58 +0300 Subject: [PATCH] Determine data types for database; add column definitions Signed-off-by: Pekka Helenius --- .../com/fjordtek/bookstore/model/Author.java | 24 ++++++++++++------- .../com/fjordtek/bookstore/model/Book.java | 15 +++++++++--- .../fjordtek/bookstore/model/BookHash.java | 3 ++- .../fjordtek/bookstore/model/Category.java | 7 +++++- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/Author.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/Author.java index 5b0ef44..da371e6 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/Author.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/Author.java @@ -43,7 +43,11 @@ public class Author { private Long id; ////////// - @Column(name = "firstname", nullable = false) + @Column( + name = "firstname", + nullable = false, + columnDefinition = "NVARCHAR(" + strMax + ")" + ) @Size( min = strMin, max = strMax, message = "First name length must be " + strMin + "-" + strMax + " characters" @@ -58,7 +62,11 @@ public class Author { private String firstName; ////////// - @Column(name = "lastname", nullable = false) + @Column( + name = "lastname", + nullable = false, + columnDefinition = "NVARCHAR(" + strMax + ")" + ) @Size( min = strMin, max = strMax, message = "Last name length must be " + strMin + "-" + strMax + " characters" @@ -74,14 +82,14 @@ public class Author { // Omit from Jackson JSON serialization //@JsonBackReference(value = "books") - @JsonIgnore + @OneToMany( - mappedBy = "author", - // We consider EAGER FetchType for updatable tables, i.e. when adding new author - fetch = FetchType.EAGER, - cascade = CascadeType.ALL, - targetEntity = Book.class + mappedBy = "author", + fetch = FetchType.LAZY, + cascade = CascadeType.ALL, + targetEntity = Book.class + //orphanRemoval = true ) private List books; diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java index 67831c9..dea04c3 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java @@ -99,7 +99,10 @@ public class Book { // Attributes with hard-coded constraints ////////// - @Column(nullable = false) + @Column( + nullable = false, + columnDefinition = "NVARCHAR(" + strMax + ")" + ) @Size( min = strMin, max = strMax, message = "Title length must be " + strMin + "-" + strMax + " characters" @@ -139,7 +142,10 @@ public class Book { // private Timestamp year; // ... // TODO: Consider allowing 0 value if year is not known - @Column(nullable = true) + @Column( + nullable = false, + columnDefinition = "INT(4)" + ) @Min( value = yearMin, message = "Minimum allowed year: " + yearMin @@ -148,7 +154,10 @@ public class Book { private int year; ////////// - @Column(unique = true, nullable = false) + @Column( + unique = true, + nullable = false + ) @NotBlank( message = "Fill the ISBN code form" ) diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/BookHash.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/BookHash.java index 96201f1..4fc222c 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/BookHash.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/BookHash.java @@ -75,7 +75,8 @@ public class BookHash { name = "hash_id", unique = true, columnDefinition = "CHAR(32)", - updatable = false + updatable = false, + nullable = false ) private String hashId; diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java index 7c3629c..b3ab1d9 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java @@ -5,6 +5,7 @@ package com.fjordtek.bookstore.model; import java.util.List; import javax.persistence.CascadeType; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; @@ -34,12 +35,16 @@ public class Category { //////////////////// // Attributes with hard-coded constraints + @Column( + nullable = false, + columnDefinition = "NVARCHAR(50)" + ) private String name; // Omit from Jackson JSON serialization //@JsonBackReference(value = "books") - @JsonIgnore + @OneToMany( mappedBy = "category", fetch = FetchType.LAZY,