Hibernate - managing insert and update queries

Consider the following association book contains chapters of OneToMany

If I do:

session.save(book)
session.save(chapter)
session.getTransaction().commit()

Hibernate generates an insert request for a book and inserts a request for a chapter

But if I do:

session.save(chapter)
session.save(book)
session.getTransaction().commit()

Hibernate executes an insert request for a chapter, inserts a request for a book, and an update request for a chapter.

Is there a way to do this in 2 inserts instead of 2 inserts and 1 update? (Assume that primary key generation is similar to Identity and Chapter.Book cannot be zero)

+3
source share
1 answer

, , , Book 1..n Chapter, cascade ( ) PERSIST. , , , .

, . ( )

, (, hashCode() equals()), save() .

. .

+4

Source: https://habr.com/ru/post/1742819/


All Articles