Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate select before update

In my work i'm using spring with exidirect, hibernate on the server side and extjs on client side. When i post a form, on the server side spring converts it to the entity. Entity has a id field, that supposes update operation. I'm calling service save method, but instead one sql update query i get many many select queries and just then update. It takes much time. And there is no need for this operation. I was looking similar questions and was trying to use persist method. This case i get error: detached entity passed to persist.

I do not have enough experience of hibernate. May be i need to configure related entities (OneToMany, ManyToOne and cascade types). Entities are generated by Spring roo tool.

Any suggestions ? Thank you.

like image 776
Damask Avatar asked Apr 30 '13 15:04

Damask


People also ask

Why does Hibernate SELECT before update?

When you create an Entity with Spring, that Entity is detached and Hibernate performs these SELECT statements because it needs to attach your Entity before persisting it.

How do I stop Spring Data JPA from doing a select before a save?

An easy way is to make your Entity implements Persistable (instead of Serializable), which will make you implement the method "isNew".


1 Answers

This is not a final answer for your question but I hope it can work as a high level guideline.

When you create an Entity with Spring, that Entity is detached and Hibernate performs these SELECT statements because it needs to attach your Entity before persisting it. Additionally, as far as I know any attempt to attach an Entity is going to trigger SELECT statements. Therefore, I strongly believe that there is not any way to save/persist your detached Entities without these SELECT statements (I might be wrong here).

If you are concern about performance you can try adding some cache functionality to your application. Moreover, you can also include JDBC solutions only for operations which are suffering with performance.

like image 100
Rafa Avatar answered Oct 31 '22 15:10

Rafa