Skip to content
Advertisement

Many to many relationship doesn’t add new Entity into the table

I have two entities Book and BookTag. They are connect by many to many relationship.

JavaScript

I try to add to the set of bookTags new BookTag entity using such service method

JavaScript

But it doesn’t work, after I saved an entity into the mysql database it work correctly view of books_tags table after inserting a new tag to the book

After I try to add new tag to the book using the method above I get such result view of books_tags table after adding a new tag to the book

As you can see my method doesn’t add new entity, it changes an existing entry for a new bookTag ID. Help me please solve this question.

Advertisement

Answer

This line could actually return an unsaved Book tag: BookTag bookTag = bookTagService.findBookTagByTagName(tag).orElse(new BookTag(tag));

so call save on bookTag before adding it to the complete Book.

Advertisement