Home > Net >  Why is the method save/saveOrUpdate in hibernate deprecated?
Why is the method save/saveOrUpdate in hibernate deprecated?

Time:10-19

I honestly had to research a lot before asking this question, was never able to find an answer. Using Hibernate 6.1 here, and most of the articles/tutorials on the internet use older versions of hibernate were most of the methods were not deprecated. Even though using saveOrUpdate method worked fine for me. If it is deprecated, there must be a reason. Even the official hibernate documentation of the latest version uses it. (If I'm at the correct documentation). I've read another question here, and they said that you don't even need to use save or saveOrUpdate, as hibernate will automatically flush any updates made to the fields inside the object. But that is not happening even after updating a value in the object, and manually flushing/refreshing or comitting the transaction.

Assistance will be appreciated, thank you!

CodePudding user response:

Just browsing the source code of Hibernate yields this commit as the one that deprecated the methods. The commit is part of this PR which mentions the corresponding issue. The reasoning for deprecating the issues is as follows:

The update() and saveOrUpdate() operations were, essentially, a failed experiment from early in the history of Hibernate, and were replaced by merge() in the JPA spec. It seems clear to me that users have had many fewer problems with merge() than with update(), and I think we probably should have deprecated these operations long ago.

The save() operation is almost identical to saveOrUpdate() except that it returns an id. You can use it like persist(), but I think persist() is a better name anyway.

You can look into the issue for more discussion on the topic.

I've read another question here, and they said that you don't even need to use save or saveOrUpdate, as hibernate will automatically flush any updates made to the fields inside the object.

I have never heard of this behavior. You should probably use persist() as an alternative to save()

  • Related