Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "update without select" possible with Hibernate/JPA?

So with JPA/Hibernate you can certainly load an entity "proxy" without hitting the database using something like session.load() or entityManager.getReference().

However, it seems it's impossible to set a property on these "proxies" without Hibernate initializing the proxy from the database. Therefore, you can't persist just the changed values (via @DynamicUpdate on the entity) without a select.

I believe this is just the way it is and if you want update without select you have to roll it yourself. I'd be delighted if somebody could prove me wrong! Am I missing something?

like image 441
rogiller Avatar asked Aug 19 '17 21:08

rogiller


People also ask

Which method is used for update operation in JPA?

Using the @Modifying Annotation As we can see, this method returns an integer. It's a feature of Spring Data JPA @Modifying queries that provides us with the number of updated entities.

Does hibernate update if no changes?

No. Strictly speaking, Hibernate does not send an SQL update on update . update simply updates the object in the current session. Hibernate executes queries when the session is flushed.

Can we use JPA without hibernate?

JPA can be used without a JPA provider aka Hibernate, EclipseLink and so on only if the application server has already a JPA implementation.

Can we use JPA and hibernate together?

You cant actually use both of them in the same application. For backwards compatibility. We are expanding our application and want to start using Spring Data JPA, but still keep the old hibernate implementation. Its better you develop your new application as a separate microservice and use spring data jpa ..


1 Answers

I'm afraid you are correct, as written in the java-doc of @DynamicUpdate: "Note, for re-attachment of detached entities this is not possible without select-before-update being enabled."

The answer given by nicolasl is not correct for this case, implementing persistable is required if you wish to control whether persist or merge is triggered when using CrudRepository.save()

like image 72
Uri Loya Avatar answered Sep 26 '22 05:09

Uri Loya