1

I am implementing a book store RESTful web app. What I am facing as an issue with @OneToMany relationship mapping simply is the INSERT operation which works only for a list containing just one Tag. When I try with more than one element it throws an Exception.

I read many post on the same topic and it seams that other guys are facing similar issues but the solutions of their problems doesn't work for me. I tried using mappedBy as well but no difference.

Here is the source-code and some trace-logs:

ENTITIES:

@Entity
@Table(name = "BOOK")
public class Book implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "BOOK_ID", nullable = false, unique = true)
    private Long id;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "CATEGORY_ID")
    private Category category;
    @Column(name = "NAME", nullable = false)
    private String name;
    @ElementCollection
    @Column(name = "PHOTO_URLS", nullable = false)
    private List<String> photoUrls;
    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "BOOK_TAG", joinColumns = @JoinColumn(name = "BOOK_ID"), inverseJoinColumns = @JoinColumn(name = "TAG_ID"))
    private List<Tag> tags;
    @Column(name = "STATUS")
    @JsonSerialize(using = BookStatusJSONSerializer.class)
    @Enumerated(EnumType.STRING)
    private BookStatus status;

    // Getter & Setters here

}

@Entity
@Table(name = "TAG")
public class Tag implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "TAG_ID", nullable = false, unique = true)
    private Integer id;
    @Column(name = "NAME", nullable = false, unique = true)
    private String name;

    // Getter & Setters here

}

@Entity
@Table(name = "CATEGORY")
public class Category implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "CATEGORY_ID", unique = true)
    private Integer id;
    @Column(name = "NAME", unique = true)
    private String name;

    // Getter & Setters here

}

SUCCESSFUL INSERT OPERATION with just one tag:

{
    "id":1,
    "category":
    {
        "id":1,
        "name":"string1"
    },
    "name":"string1",
    "photoUrls":
    [
        "string1",
        "string2",
        "string3"
    ],
    "tags":
    [
        {
            "id":1,
            "name":"string1"
        }
    ],
    "status":"available"
}

2017-02-17 08:54:41.228 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.t.internal.TransactionImpl         : begin
2017-02-17 08:54:41.229 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.loader.Loader              : Loading entity: [com.rbc.bookstore.entities.Book#1]
2017-02-17 08:54:41.230 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.SQL                        : select book0_."book_id" as book_id1_2_2_, book0_."category_id" as category4_2_2_, book0_."name" as name2_2_2_, book0_."status" as status3_2_2_, category1_."category_id" as category1_0_0_, category1_."name" as name2_0_0_, tags2_."tag_id" as tag_id1_4_4_, tags2_."tag_id" as tag_id1_4_1_, tags2_."name" as name2_4_1_ from "book" book0_ left outer join "category" category1_ on book0_."category_id"=category1_."category_id" left outer join "tag" tags2_ on book0_."book_id"=tags2_."tag_id" where book0_."book_id"=?
2017-02-17 08:54:41.233 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.loader.Loader              : Result set row: 0
2017-02-17 08:54:41.233 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.loader.Loader              : Result row: EntityKey[com.rbc.bookstore.entities.Category#1], EntityKey[com.rbc.bookstore.entities.Tag#1], EntityKey[com.rbc.bookstore.entities.Book#1]
2017-02-17 08:54:41.234 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.loader.Loader              : Found row of collection: [com.rbc.bookstore.entities.Book.tags#1]
2017-02-17 08:54:41.235 DEBUG 11404 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad         : Resolving associations for [com.rbc.bookstore.entities.Category#1]
2017-02-17 08:54:41.235 DEBUG 11404 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad         : Done materializing entity [com.rbc.bookstore.entities.Category#1]
2017-02-17 08:54:41.235 DEBUG 11404 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad         : Resolving associations for [com.rbc.bookstore.entities.Tag#1]
2017-02-17 08:54:41.235 DEBUG 11404 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad         : Done materializing entity [com.rbc.bookstore.entities.Tag#1]
2017-02-17 08:54:41.235 DEBUG 11404 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad         : Resolving associations for [com.rbc.bookstore.entities.Book#1]
2017-02-17 08:54:41.236 DEBUG 11404 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad         : Done materializing entity [com.rbc.bookstore.entities.Book#1]
2017-02-17 08:54:41.236 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.l.internal.CollectionLoadContext   : 1 collections were found in result set for role: com.rbc.bookstore.entities.Book.tags
2017-02-17 08:54:41.236 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.l.internal.CollectionLoadContext   : Collection fully initialized: [com.rbc.bookstore.entities.Book.tags#1]
2017-02-17 08:54:41.236 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.l.internal.CollectionLoadContext   : 1 collections initialized for role: com.rbc.bookstore.entities.Book.tags
2017-02-17 08:54:41.237 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.loader.Loader              : Done entity load
2017-02-17 08:54:41.238 DEBUG 11404 --- [nio-8080-exec-2] stractLoadPlanBasedCollectionInitializer : Loading collection: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 08:54:41.238 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.SQL                        : select photourls0_."book_book_id" as book_book_1_3_0_, photourls0_."photo_urls" as photo_ur2_3_0_ from "book_photo_urls" photourls0_ where photourls0_."book_book_id"=?
2017-02-17 08:54:41.240 DEBUG 11404 --- [nio-8080-exec-2] o.h.l.p.e.p.i.ResultSetProcessorImpl     : Preparing collection intializer : [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 08:54:41.240 DEBUG 11404 --- [nio-8080-exec-2] o.h.l.p.e.p.i.ResultSetProcessorImpl     : Starting ResultSet row #0
2017-02-17 08:54:41.240 DEBUG 11404 --- [nio-8080-exec-2] e.p.i.CollectionReferenceInitializerImpl : Found row of collection: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 08:54:41.241 DEBUG 11404 --- [nio-8080-exec-2] o.h.l.p.e.p.i.ResultSetProcessorImpl     : Starting ResultSet row #1
2017-02-17 08:54:41.241 DEBUG 11404 --- [nio-8080-exec-2] e.p.i.CollectionReferenceInitializerImpl : Found row of collection: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 08:54:41.241 DEBUG 11404 --- [nio-8080-exec-2] o.h.l.p.e.p.i.ResultSetProcessorImpl     : Starting ResultSet row #2
2017-02-17 08:54:41.241 DEBUG 11404 --- [nio-8080-exec-2] e.p.i.CollectionReferenceInitializerImpl : Found row of collection: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 08:54:41.242 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.l.internal.CollectionLoadContext   : 1 collections were found in result set for role: com.rbc.bookstore.entities.Book.photoUrls
2017-02-17 08:54:41.242 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.l.internal.CollectionLoadContext   : Collection fully initialized: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 08:54:41.242 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.l.internal.CollectionLoadContext   : 1 collections initialized for role: com.rbc.bookstore.entities.Book.photoUrls
2017-02-17 08:54:41.242 DEBUG 11404 --- [nio-8080-exec-2] o.h.r.j.i.ResourceRegistryStandardImpl   : HHH000387: ResultSet's statement was not registered
2017-02-17 08:54:41.242 DEBUG 11404 --- [nio-8080-exec-2] stractLoadPlanBasedCollectionInitializer : Done loading collection
2017-02-17 08:54:41.243 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.t.internal.TransactionImpl         : committing
2017-02-17 08:54:41.243 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.i.AbstractFlushingEventListener    : Processing flush-time cascades
2017-02-17 08:54:41.244 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.i.AbstractFlushingEventListener    : Dirty checking collections
2017-02-17 08:54:41.244 DEBUG 11404 --- [nio-8080-exec-2] o.hibernate.engine.spi.CollectionEntry   : Collection dirty: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 08:54:41.244 DEBUG 11404 --- [nio-8080-exec-2] o.hibernate.engine.spi.CollectionEntry   : Collection dirty: [com.rbc.bookstore.entities.Book.tags#1]
2017-02-17 08:54:41.245 DEBUG 11404 --- [nio-8080-exec-2] o.hibernate.engine.internal.Collections  : Collection found: [com.rbc.bookstore.entities.Book.photoUrls#1], was: [com.rbc.bookstore.entities.Book.photoUrls#1] (initialized)
2017-02-17 08:54:41.245 DEBUG 11404 --- [nio-8080-exec-2] o.hibernate.engine.internal.Collections  : Collection found: [com.rbc.bookstore.entities.Book.tags#1], was: [com.rbc.bookstore.entities.Book.tags#1] (initialized)
2017-02-17 08:54:41.245 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.i.AbstractFlushingEventListener    : Flushed: 0 insertions, 0 updates, 0 deletions to 3 objects
2017-02-17 08:54:41.245 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.i.AbstractFlushingEventListener    : Flushed: 0 (re)creations, 2 updates, 0 removals to 2 collections
2017-02-17 08:54:41.245 DEBUG 11404 --- [nio-8080-exec-2] o.hibernate.internal.util.EntityPrinter  : Listing entities:
2017-02-17 08:54:41.245 DEBUG 11404 --- [nio-8080-exec-2] o.hibernate.internal.util.EntityPrinter  : com.rbc.bookstore.entities.Category{name=string1, id=1}
2017-02-17 08:54:41.245 DEBUG 11404 --- [nio-8080-exec-2] o.hibernate.internal.util.EntityPrinter  : com.rbc.bookstore.entities.Tag{name=string1, id=1}
2017-02-17 08:54:41.245 DEBUG 11404 --- [nio-8080-exec-2] o.hibernate.internal.util.EntityPrinter  : com.rbc.bookstore.entities.Book{photoUrls=[string1, string2, string3], name=doggie, id=1, category=com.rbc.bookstore.entities.Category#1, status=AVAILABLE, tags=[com.rbc.bookstore.entities.Tag#1]}
2017-02-17 08:54:41.246 DEBUG 11404 --- [nio-8080-exec-2] o.h.p.c.AbstractCollectionPersister      : Deleting collection: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 08:54:41.246 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.SQL                        : delete from "book_photo_urls" where "book_book_id"=?
2017-02-17 08:54:41.247 DEBUG 11404 --- [nio-8080-exec-2] o.h.p.c.AbstractCollectionPersister      : Done deleting collection
2017-02-17 08:54:41.247 DEBUG 11404 --- [nio-8080-exec-2] o.h.p.c.AbstractCollectionPersister      : Inserting collection: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 08:54:41.248 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.SQL                        : insert into "book_photo_urls" ("book_book_id", "photo_urls") values (?, ?)
2017-02-17 08:54:41.248 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.SQL                        : insert into "book_photo_urls" ("book_book_id", "photo_urls") values (?, ?)
2017-02-17 08:54:41.249 DEBUG 11404 --- [nio-8080-exec-2] org.hibernate.SQL                        : insert into "book_photo_urls" ("book_book_id", "photo_urls") values (?, ?)
2017-02-17 08:54:41.250 DEBUG 11404 --- [nio-8080-exec-2] o.h.p.c.AbstractCollectionPersister      : Done inserting collection: 3 rows inserted
2017-02-17 08:54:41.250 DEBUG 11404 --- [nio-8080-exec-2] o.h.p.c.AbstractCollectionPersister      : Deleting rows of collection: [com.rbc.bookstore.entities.Book.tags#1]
2017-02-17 08:54:41.251 DEBUG 11404 --- [nio-8080-exec-2] o.h.p.c.AbstractCollectionPersister      : No rows to delete
2017-02-17 08:54:41.251 DEBUG 11404 --- [nio-8080-exec-2] o.h.p.c.AbstractCollectionPersister      : Inserting rows of collection: [com.rbc.bookstore.entities.Book.tags#1]
2017-02-17 08:54:41.251 DEBUG 11404 --- [nio-8080-exec-2] o.h.p.c.AbstractCollectionPersister      : Done inserting rows: 0 inserted
2017-02-17 08:54:41.253 DEBUG 11404 --- [nio-8080-exec-2] o.h.e.jdbc.internal.JdbcCoordinatorImpl  : HHH000420: Closing un-released batch

...and HERE IS AN UNSUCCESSFUL INSERT OPERATION with two tags:

{
    "id":1,
    "category":
    {
        "id":1,
        "name":"string1"
    },
    "name":"string1",
    "photoUrls":
    [
        "string1",
        "string2",
        "string3"
    ],
    "tags":
    [
        {
            "id":1,
            "name":"string1"
        },
        {
            "id":2,
            "name":"string2"
        }
    ],
    "status":"available"
}

2017-02-17 09:31:03.662 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.t.internal.TransactionImpl         : begin
2017-02-17 09:31:03.664 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.loader.Loader              : Loading entity: [com.rbc.bookstore.entities.Book#1]
2017-02-17 09:31:03.665 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.SQL                        : select book0_."book_id" as book_id1_2_2_, book0_."category_id" as category4_2_2_, book0_."name" as name2_2_2_, book0_."status" as status3_2_2_, category1_."category_id" as category1_0_0_, category1_."name" as name2_0_0_, tags2_."tag_id" as tag_id1_4_4_, tags2_."tag_id" as tag_id1_4_1_, tags2_."name" as name2_4_1_ from "book" book0_ left outer join "category" category1_ on book0_."category_id"=category1_."category_id" left outer join "tag" tags2_ on book0_."book_id"=tags2_."tag_id" where book0_."book_id"=?
2017-02-17 09:31:03.667 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.loader.Loader              : Result set row: 0
2017-02-17 09:31:03.667 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.loader.Loader              : Result row: EntityKey[com.rbc.bookstore.entities.Category#1], EntityKey[com.rbc.bookstore.entities.Tag#1], EntityKey[com.rbc.bookstore.entities.Book#1]
2017-02-17 09:31:03.668 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.loader.Loader              : Found row of collection: [com.rbc.bookstore.entities.Book.tags#1]
2017-02-17 09:31:03.668 DEBUG 11404 --- [nio-8080-exec-5] o.h.engine.internal.TwoPhaseLoad         : Resolving associations for [com.rbc.bookstore.entities.Category#1]
2017-02-17 09:31:03.668 DEBUG 11404 --- [nio-8080-exec-5] o.h.engine.internal.TwoPhaseLoad         : Done materializing entity [com.rbc.bookstore.entities.Category#1]
2017-02-17 09:31:03.668 DEBUG 11404 --- [nio-8080-exec-5] o.h.engine.internal.TwoPhaseLoad         : Resolving associations for [com.rbc.bookstore.entities.Tag#1]
2017-02-17 09:31:03.669 DEBUG 11404 --- [nio-8080-exec-5] o.h.engine.internal.TwoPhaseLoad         : Done materializing entity [com.rbc.bookstore.entities.Tag#1]
2017-02-17 09:31:03.669 DEBUG 11404 --- [nio-8080-exec-5] o.h.engine.internal.TwoPhaseLoad         : Resolving associations for [com.rbc.bookstore.entities.Book#1]
2017-02-17 09:31:03.669 DEBUG 11404 --- [nio-8080-exec-5] o.h.engine.internal.TwoPhaseLoad         : Done materializing entity [com.rbc.bookstore.entities.Book#1]
2017-02-17 09:31:03.669 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.l.internal.CollectionLoadContext   : 1 collections were found in result set for role: com.rbc.bookstore.entities.Book.tags
2017-02-17 09:31:03.670 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.l.internal.CollectionLoadContext   : Collection fully initialized: [com.rbc.bookstore.entities.Book.tags#1]
2017-02-17 09:31:03.670 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.l.internal.CollectionLoadContext   : 1 collections initialized for role: com.rbc.bookstore.entities.Book.tags
2017-02-17 09:31:03.670 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.loader.Loader              : Done entity load
2017-02-17 09:31:03.671 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.loader.Loader              : Loading entity: [com.rbc.bookstore.entities.Tag#2]
2017-02-17 09:31:03.671 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.SQL                        : select tag0_."tag_id" as tag_id1_4_0_, tag0_."name" as name2_4_0_ from "tag" tag0_ where tag0_."tag_id"=?
2017-02-17 09:31:03.673 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.loader.Loader              : Done entity load
2017-02-17 09:31:03.673 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.i.AbstractSaveEventListener        : Generated identifier: 2, using strategy: org.hibernate.id.Assigned
2017-02-17 09:31:03.674 DEBUG 11404 --- [nio-8080-exec-5] stractLoadPlanBasedCollectionInitializer : Loading collection: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 09:31:03.674 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.SQL                        : select photourls0_."book_book_id" as book_book_1_3_0_, photourls0_."photo_urls" as photo_ur2_3_0_ from "book_photo_urls" photourls0_ where photourls0_."book_book_id"=?
2017-02-17 09:31:03.675 DEBUG 11404 --- [nio-8080-exec-5] o.h.l.p.e.p.i.ResultSetProcessorImpl     : Preparing collection intializer : [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 09:31:03.676 DEBUG 11404 --- [nio-8080-exec-5] o.h.l.p.e.p.i.ResultSetProcessorImpl     : Starting ResultSet row #0
2017-02-17 09:31:03.676 DEBUG 11404 --- [nio-8080-exec-5] e.p.i.CollectionReferenceInitializerImpl : Found row of collection: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 09:31:03.676 DEBUG 11404 --- [nio-8080-exec-5] o.h.l.p.e.p.i.ResultSetProcessorImpl     : Starting ResultSet row #1
2017-02-17 09:31:03.676 DEBUG 11404 --- [nio-8080-exec-5] e.p.i.CollectionReferenceInitializerImpl : Found row of collection: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 09:31:03.676 DEBUG 11404 --- [nio-8080-exec-5] o.h.l.p.e.p.i.ResultSetProcessorImpl     : Starting ResultSet row #2
2017-02-17 09:31:03.676 DEBUG 11404 --- [nio-8080-exec-5] e.p.i.CollectionReferenceInitializerImpl : Found row of collection: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 09:31:03.676 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.l.internal.CollectionLoadContext   : 1 collections were found in result set for role: com.rbc.bookstore.entities.Book.photoUrls
2017-02-17 09:31:03.677 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.l.internal.CollectionLoadContext   : Collection fully initialized: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 09:31:03.677 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.l.internal.CollectionLoadContext   : 1 collections initialized for role: com.rbc.bookstore.entities.Book.photoUrls
2017-02-17 09:31:03.677 DEBUG 11404 --- [nio-8080-exec-5] o.h.r.j.i.ResourceRegistryStandardImpl   : HHH000387: ResultSet's statement was not registered
2017-02-17 09:31:03.677 DEBUG 11404 --- [nio-8080-exec-5] stractLoadPlanBasedCollectionInitializer : Done loading collection
2017-02-17 09:31:03.677 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.t.internal.TransactionImpl         : committing
2017-02-17 09:31:03.678 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.i.AbstractFlushingEventListener    : Processing flush-time cascades
2017-02-17 09:31:03.678 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.i.AbstractFlushingEventListener    : Dirty checking collections
2017-02-17 09:31:03.678 DEBUG 11404 --- [nio-8080-exec-5] o.hibernate.engine.spi.CollectionEntry   : Collection dirty: [com.rbc.bookstore.entities.Book.photoUrls#1]
2017-02-17 09:31:03.679 DEBUG 11404 --- [nio-8080-exec-5] o.hibernate.engine.spi.CollectionEntry   : Collection dirty: [com.rbc.bookstore.entities.Book.tags#1]
2017-02-17 09:31:03.679 DEBUG 11404 --- [nio-8080-exec-5] o.hibernate.engine.internal.Collections  : Collection found: [com.rbc.bookstore.entities.Book.photoUrls#1], was: [com.rbc.bookstore.entities.Book.photoUrls#1] (initialized)
2017-02-17 09:31:03.679 DEBUG 11404 --- [nio-8080-exec-5] o.hibernate.engine.internal.Collections  : Collection found: [com.rbc.bookstore.entities.Book.tags#1], was: [com.rbc.bookstore.entities.Book.tags#1] (initialized)
2017-02-17 09:31:03.680 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.i.AbstractFlushingEventListener    : Flushed: 1 insertions, 0 updates, 0 deletions to 4 objects
2017-02-17 09:31:03.680 DEBUG 11404 --- [nio-8080-exec-5] o.h.e.i.AbstractFlushingEventListener    : Flushed: 0 (re)creations, 2 updates, 0 removals to 2 collections
2017-02-17 09:31:03.680 DEBUG 11404 --- [nio-8080-exec-5] o.hibernate.internal.util.EntityPrinter  : Listing entities:
2017-02-17 09:31:03.680 DEBUG 11404 --- [nio-8080-exec-5] o.hibernate.internal.util.EntityPrinter  : com.rbc.bookstore.entities.Category{name=string1, id=1}
2017-02-17 09:31:03.680 DEBUG 11404 --- [nio-8080-exec-5] o.hibernate.internal.util.EntityPrinter  : com.rbc.bookstore.entities.Tag{name=string1, id=1}
2017-02-17 09:31:03.681 DEBUG 11404 --- [nio-8080-exec-5] o.hibernate.internal.util.EntityPrinter  : com.rbc.bookstore.entities.Book{photoUrls=[string1, string2, string3], name=doggie, id=1, category=com.rbc.bookstore.entities.Category#1, status=AVAILABLE, tags=[com.rbc.bookstore.entities.Tag#1, com.rbc.bookstore.entities.Tag#2]}
2017-02-17 09:31:03.681 DEBUG 11404 --- [nio-8080-exec-5] o.hibernate.internal.util.EntityPrinter  : com.rbc.bookstore.entities.Tag{name=string2, id=2}
2017-02-17 09:31:03.681 DEBUG 11404 --- [nio-8080-exec-5] org.hibernate.SQL                        : insert into "tag" ("name", "tag_id") values (?, ?)
2017-02-17 09:31:03.689 DEBUG 11404 --- [nio-8080-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper   : could not execute statement [n/a]

java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: foreign key no parent; "FK54oiqofxlntc22v49amlljiw" table: "tag"
    .....

Here is an UML diagram of DB relationships: enter image description here

nenito
  • 1,214
  • 6
  • 19
  • 33
  • 1
    I think that you got your mapping wrong. In this link there is a good explanation by Oscar López http://stackoverflow.com/questions/11938253/jpa-joincolumn-vs-mappedby – RubioRic Feb 17 '17 at 07:54
  • @RubioRic: I got your point. I tried to keep Tag entity without any additional data from Book entity and to store the reference to Tag into Book but actually I need to do the exact opposite thing. Thank you for pointing me out the wrong idea I had! – nenito Feb 17 '17 at 08:19
  • @RubioRic: I guest I was celebrating too early :). I managed to pass more than one tag but now in order to have all related Tag's values available in Book I'll need to make additional call to DB fetching them from Tag entity. – nenito Feb 17 '17 at 08:47
  • Do you mean that you have to launch additional queries or it's JPA doing it in the background? If it's the first option, maybe you still have something wrong. If it's the second one, maybe you can tune your mapping establishing a fetching strategy. – RubioRic Feb 17 '17 at 08:57
  • @RubioRic: I need to call explicitly Tag entity to fetch the data and place it in Book. What changes I made you can refer to Book and Tag entities. – nenito Feb 17 '17 at 09:41
  • I do not undestand. Yes, you have to create new instances or retrieve the already created Tags from DB to establish the relation with Book. I don't know other way around. – RubioRic Feb 17 '17 at 09:48
  • @RubioRic: The bookId field of Tag entity is populated with nulls which is weird. Something is not quite right. – nenito Feb 17 '17 at 09:49

0 Answers0