So when we persist the entity and then if we change the data of entity it will sync, then whats the difference between merge and persist as i see both are doing the same
@pradeepsingh-kr2hvАй бұрын
Subscribed, thanks for good explanation
@the_codealchemistАй бұрын
Thanks!
@sree64846 ай бұрын
Really liked your video, you earned a subscriber
@the_codealchemist6 ай бұрын
Thanks!
@sree64846 ай бұрын
When i detach an object from context, changes made to that object wont be tracked lets assume i did some changes to it, what if i want to set the object properties back to the changes tracked by the hibernate? I can't use refresh here because assuming that record in database is also changed and if i do refresh object data in context gets refreshed with data from db and i don't want that, what to do in this scenario?
@the_codealchemist6 ай бұрын
Correct, refresh means you want to refresh it from the DB. In this case, you can use merge to reattach the object.
@sree64846 ай бұрын
@@the_codealchemist if I use merge, untracked changes gets merged with the context but I don't want those untracked changes I want to set my object properties back to same the properties which were last tracked by the hibernate. I'll also go through your practical video to get more understanding on this.
@the_codealchemist6 ай бұрын
I think I got your point. You basically want to revert the object to it's last tracked state without merging untracked changes and refreshing from the DB. AFAIK, Hibernate doesn't handle it automatically so you'll have to do it manually e.g. saving the object state without detaching the entity..maybe a clone or deep copy and then using this copy to reset the state when you want to track again.
@universal43345 ай бұрын
Entity manager factory is created at boot up time. So, when is this persistence context created ? I mean, how can it track/cache all the data that is in db. As for my understanding, it maintains the latest state, and when we do any operation, it checks with the maintained state of an object. Let's say i have a million unique objects, will persistence context maintains all of them, or how it is Will there be multiple instances of persistence context ? When it is created and destroyed 😢
@the_codealchemist5 ай бұрын
Hi, I think you're bit confused with EntityManager and persistence context..persistence context is a short-lived, transaction-scoped cache that manages the state of entities within a specific transaction or unit of work, rather than caching the entire database like any other caching solution. So it's different from any cache that you might know like Redis, Caffiene. Second, it's created automatically when you create a new instance of EntityManager(similar to Hibernate session) which is not created at startup..that is EntityManagerFactory which is created at server startup(similar to Hiernate's SessionFactory). So it doesn't cache all the data, only the data associated with that transaction within the scope of that entity manager and then there's equality check using hashcode and equals so it won't cache duplicates.