Damask
Damask

Reputation: 1254

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.

Upvotes: 5

Views: 17303

Answers (2)

Dmitry
Dmitry

Reputation: 1096

Add @SelectBeforeUpdate(false) annotation to the entity.

Upvotes: 1

Rafa
Rafa

Reputation: 2027

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.

Upvotes: 8

Related Questions