JPA vs Hibernate : The difference between save, persist, merge and update

preview_player
Показать описание
JPA and Hibernate provide different methods to persist new and to update existing entities. You can choose between JPA’s persist and merge and Hibernate’s save and update methods.
It seems like there are 2 pairs of 2 methods that do the same. You can use the methods persist and save to store a new entity and the methods merge and update to store the changes of a detached entity in the database. That’s why a lot of developers are wondering which of these methods they should use. Let’s take a closer look at the details and small differences of these methods.

If you like this video, please give me your thumbs up and share it with your friends and co-workers.

Like my channel? Subscribe!

Join the free Member Library:

Want to connect with me?
Рекомендации по теме
Комментарии
Автор

00:52 JPA entity life-cycle state
1:27 *transient state*
1:33 call entityManager.persist(entity) to Managed state
1:57 *Detached* state
2:54 persist or save
2:58 transient - persist/save -> managed
3:03 you need to attach the entity to the persistence context
3:14 use either entitymanager.persist or hibernate.save
4:22 *EntityManager.persis documentation does not define at all when the primary key is generated after the call*
6:17 code demo
9:30 update or merge
10:54 code demo
11:07 after em.close(), the *entity a is detached*
11:30 see what happens with *merge*

ruixue
Автор

I put the speed on x1.25.
One point of feedback: sometimes it is rather difficult to follow, because you need to reproduce the code in your mind, perhaps you could have the code in a side panel to the left and the console on a side panel to the right and step-by-step follow the code and what the outcome is in the console.

jurvanoerle
Автор

perfect content on JPA, HIbernate from Thorben Janssen as usual.

SreeRamakrishnaNagulakonda
Автор

Thanks, finally understood what the merge really does. very insiduous because it set all fields ;)

zakb.
Автор

Hi sir,
I didn't quite understand what you said from 05:25 to 05:45 ?
Also, is it possible to persist an entity without active transaction ??
I mean the entities get persisted when we commit the transaction or when we flush the session right ??

saaryamohapatra
Автор

@5:10. sir can you please confirm if its save() or persist at the mentioned timestamp. i am little confused with this actually.

anilreddy
Автор

Very good explanation, i'm trainer also on java technologies and i'm really very pleased to watch all your videos sir, please continue with the same pace, i'm getting many requests about spring integration framework but due to my load's this year I can't perform such cours, please if you can prepare a cours on the EIP using spring integration DSL. it will very help full for the community. thank's in advance.

boutalebzoheir
Автор

You said update method throws an exception but it didnt throw any exception in ur video

gouthamreddy
Автор

Nice video sir. Please can you do a complete simple video with JavaFx, Spring MVC and Hibernate. I like you way of teaching.

getsglobalengineering
Автор

very nice explanation but i request one thing to show all cases, like you left update cases. Thanks

malam
Автор

if we accessed the code samples, that would be great!

ersenoztoprak
Автор

Hibernate 6 Deprecated Merge and Save 👍

techcoding
Автор

Hello, sir.I am Junior Java Developer and i have a problem.I have a restful api which returns Page<Post>.Problem is about consuming this Page<Post> in resttemplate.Can you reccommend me any book or resource about this? (except Stackoverflow) :)

fuadshirinov
Автор

Is it ok to use merge on a entity after it is saved?

Jeptxxle
Автор

How to use persist in Spring data jpa than save()?

shakilmith
Автор

You never saved or update the Author a2 anywhere . How the First name 'Janssen' will be saved. You mention at the end one 's' not saved.

boopathirajagopalan
Автор

sir could you please explain effect of strategy Identity in save and persist

ullas