From the course: Java Persistence with JPA and Hibernate

Unlock the full course today

Join today to access over 23,400 courses taught by industry experts.

Attaching and detaching an entity instance

Attaching and detaching an entity instance - JPA Tutorial

From the course: Java Persistence with JPA and Hibernate

Attaching and detaching an entity instance

- [Instructor] Here's another use case. What if you don't do a find for the book with an ID of one and instead create a new book, book1, like this. Book book1 equals to new Book and then set its ID to one. Book1.setId to one. Name to my newest book, book1.setName "my newest book." and ISBN to 123-456. If I run this, what do you think would happen? Well, if you check the book table, the book with an ID of one has a different name to the name that I just set in the book instance. Let's run this. Back in the book table. Well, nothing has happened. The book name is still my new book for the book with an ID of one, but the values, at least the book name of the instance, is different to what's there in the database. Shouldn't it be mirrored in the database and shouldn't the value change? Well, not really. This time, the book instance was created using the construct, so it's not in the persistence context. It's outside of the context. This means the instance isn't in the managed state, so no…

Contents