From the course: Java Persistence with JPA and Hibernate
Using the getReference() and refresh() methods - JPA Tutorial
From the course: Java Persistence with JPA and Hibernate
Using the getReference() and refresh() methods
- [Instructor] In a previous video, you saw how the find method works. When it's called, if an instance doesn't already exist in the context, it issues a query to the database to retrieve a row of data with a specified primary key. So, it gives you an entity instance for the requested ID or primary key. The getReference method also gives you an entity instance for the requested ID or primary key. However, this method never issues a SELECT query on the database. Let's try that. Inside the useGetReference method, here, I've called the getReference method of the EntityManager, passing the entity class name as Book and the ID as 2. Let's run the program. There's no query issued, but actually, it retrieves the entity that we requested. Now, let's print it out. Right here, let's print book2 and run again. Now the SELECT query magically appears. How is that? Well, with getReference, we only get a shell of the entity. This means there's no data yet in the context for it, so no query is issued to the database. However, this shell is ready to issue a query whenever needed. So, if you do anything with the instance, or in other words, use it, a query is issued to fetch data. Here, the simple use of the instance, like printing, caused a query to be issued on the database. So how is this method different from the find method? Well, getReference will always give you only a shell of an instance with no data in it in the context. But as soon as it's being used, data will be fetched by issuing a query. If the instance is never used, a query will never be sent. So, it's like lazily loading an instance with data from the database. When you practically use Hibernate in real-world applications, you might have a use case that needs this kind of behavior. So, getReference would be handy in that case. Let's check out another use case. In your program, you might want to make sure that you are working with a fresh copy of the instance. That is, in some part of the program, the values of the instance you are working with might have been changed. Now, in your algorithm, if the transaction commits, all those changes will go to the database, which is not what you intend. So, how can you make sure that the instance you are working with has no previous modifications? Well, the refresh method is there for the rescue. You can simply call the refresh method on the instance. This will send a query to the database, and the instance will get populated with the data in the database. Basically, you'll get a fresh copy of the data to work on. This is like undoing any changes that might have happened on the entity instance in the context. Let's try that. We have the Book entity, book2. Let's say someone changed the name on this Book instance to "Some Book." Now, if the transaction commits, this change will be reflected in the database, but you do not want that to happen. Call the EntityManager's refresh method to undo the changes. So here, below the line where you set the name, you call EntityManager.refresh(book2). Print the before and after values. So, above this refresh, let's print it out saying "Before book2" and after the refresh, let's again print it out saying "After book2." You need to call the useRefresh method from the main method before you run. So, let's uncomment out this one and comment this method. Use useRefresh. Let's run again. First, you get the SELECT query issued by the find call. Then, someone sets its name to a different value, that is "Some Book." So, in the context, the book name got changed to that value. That's what you get in the "Before" here. Thereafter, you called the refresh method to get the data in the way it is in the database. It sent another SELECT query to bring the data as it is now in the database. That's what you get in "After" here. So, the change that was done has been undone, and now you have fresh data as it is in the database to work with. So, that's how getReference and refresh methods work.
Contents
-
-
-
-
-
-
-
(Locked)
Finding and updating an existing entity instance7m 12s
-
(Locked)
Attaching and detaching an entity instance4m 42s
-
(Locked)
Removing an entity instance2m 42s
-
Using the getReference() and refresh() methods5m 53s
-
(Locked)
Using composite keys on an entity8m 1s
-
(Locked)
Challenge: Art Class Management app, part 31m 29s
-
(Locked)
Solution: Art Class Management app, part 34m 8s
-
(Locked)
-
-
-
-