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

Поделиться
HTML-код
  • Опубликовано: 20 дек 2017
  • 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!
    ➜ bit.ly/2cUsid8
    Join the free Member Library:
    goo.gl/dtyIIC
    Read the accompanying post: www.thoughts-on-java.org/pers...
    Want to connect with me?
    Blog: www.thoughts-on-java.org/
    Twitter: / thjanssen123
    Facebook: / thoughtsonjava
  • НаукаНаука

Комментарии • 32

  • @jurvanoerle2845
    @jurvanoerle2845 4 года назад +16

    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.

  • @ruixue6955
    @ruixue6955 3 года назад +6

    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*

  • @SreeRamakrishnaNagulakonda
    @SreeRamakrishnaNagulakonda 2 года назад +2

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

  • @salmanbaig7703
    @salmanbaig7703 3 года назад

    Excellent explanation. Thank you

  • @zakb.7108
    @zakb.7108 3 года назад

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

  • @anilreddy5009
    @anilreddy5009 5 лет назад +1

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

  • @malam3958
    @malam3958 5 лет назад

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

  • @saaryamohapatra2406
    @saaryamohapatra2406 3 года назад

    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 ??

  • @robertporto4008
    @robertporto4008 3 года назад

    Great thank you

  • @getsglobalengineering7723
    @getsglobalengineering7723 6 лет назад +3

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

    • @ThoughtsOnJava
      @ThoughtsOnJava  6 лет назад

      Thanks. I'll put it on my list with video ideas

  • @fuadshirinov2116
    @fuadshirinov2116 4 года назад

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

  • @boutalebzoheir3768
    @boutalebzoheir3768 5 лет назад

    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.

    • @ThoughtsOnJava
      @ThoughtsOnJava  5 лет назад

      Thanks.
      Unfortunately, I have already planned a lot of things for 2019 and spring integration DSL isn't part of it :(

  • @gouthamreddy2252
    @gouthamreddy2252 2 месяца назад

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

  • @ullas9999
    @ullas9999 5 лет назад

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

    • @ThoughtsOnJava
      @ThoughtsOnJava  5 лет назад

      Hi,
      I explained the Identity strategy in here
      article: thoughts-on-java.org/jpa-generate-primary-keys/
      video: ruclips.net/video/qn9SbW44rQ8/видео.html

  • @shakilmith
    @shakilmith Год назад

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

  • @ersenoztoprak4089
    @ersenoztoprak4089 5 лет назад +4

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

    • @mohamedrawoof5093
      @mohamedrawoof5093 3 года назад

      Kindly provide us the code to test and check from our local dev environment with your samples. Thanks in advance Sir

  • @techcoding9020
    @techcoding9020 Год назад

    Hibernate 6 Deprecated Merge and Save 👍

  • @Jeptxxle
    @Jeptxxle 4 года назад

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

    • @ThoughtsOnJava
      @ThoughtsOnJava  4 года назад

      After you called the save method, the entity is managed and you don't need to merge it to save your changes (see thorben-janssen.com/entity-lifecycle-model/)

  • @deepusasidharan2012
    @deepusasidharan2012 2 года назад

    good

  • @boopathirajagopalan3047
    @boopathirajagopalan3047 4 года назад

    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.

    • @ThoughtsOnJava
      @ThoughtsOnJava  4 года назад

      The changes of a managed entity get saved automatically. I epxlain that in this article: thorben-janssen.com/entity-lifecycle-model/

  • @zikaperic2133
    @zikaperic2133 4 года назад +1

    Poor explanation

    • @ThoughtsOnJava
      @ThoughtsOnJava  4 года назад

      Thanks for the feedback. Please let me know what you think is missing.

    • @zikaperic2133
      @zikaperic2133 4 года назад

      @@ThoughtsOnJava First, the material need s to be more didactic, giving more overview of the problem, what is actually Hibernate doing and why. It can illustrate by some more concrete examples, etc. so people who intend to use can imagine their applications. Then other smaller technical details on what works and what not (save, persist, etc) should be also given from more general perspective... now it looks as if it is an bad coding but probably there are deeper reasons. Lastly, it is frustrating that potential good material online (which I believe should be) it is hard to find because of the videos like this that first intention is some kind of self-promotion,

    • @ThoughtsOnJava
      @ThoughtsOnJava  4 года назад

      @@zikaperic2133 OK, thanks for explaining your opinion

    • @deivam2008
      @deivam2008 7 месяцев назад

      @@ThoughtsOnJava plz explain code level explanation spring boot with github link