From 87ccddc9eb87dcaa9a1e1132ba565b3982192189 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Thu, 24 Sep 2020 11:06:55 +0300 Subject: [PATCH] Ignore books in JSON output; define SQL fetch & cascade types for book list Signed-off-by: Pekka Helenius --- .../com/fjordtek/bookstore/model/Category.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) 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 8d98e78..f2620ef 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java @@ -4,14 +4,15 @@ package com.fjordtek.bookstore.model; import java.util.List; +import javax.persistence.CascadeType; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; -import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIgnore; @Entity @@ -21,7 +22,7 @@ public class Category { // Primary key value in database @Id @GeneratedValue( - strategy = GenerationType.AUTO, + strategy = GenerationType.IDENTITY, generator = "categoryIdGenerator" ) @SequenceGenerator( @@ -35,11 +36,15 @@ public class Category { // Attributes with hard-coded constraints private String name; - @JsonBackReference + // Omit from Jackson JSON serialization + //@JsonBackReference(value = "books") + + @JsonIgnore @OneToMany( - mappedBy = "category" - //fetch = FetchType.LAZY, - //cascade = CascadeType.ALL + mappedBy = "category", + fetch = FetchType.LAZY, + cascade = CascadeType.ALL + //targetEntity = Book.class ) private List books;