5

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.

Damask
  • 1,754
  • 1
  • 13
  • 24

2 Answers2

8

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.

Rafa
  • 1,997
  • 3
  • 21
  • 33
  • I can understand if hibernate makes one query to attach before save, but i can't understand if hibernate makes 50, 100 queries before save this one. Ajax request fails by timeout – Damask May 01 '13 at 03:00
  • Intriguing, in this case might be worth to check the OneToMany ManyToOne relationships. Is it a class with dozens of relationships? – Rafa May 01 '13 at 19:04
  • Yes, it's medical system, patients, diagnosis, prescriptions - it has many relationships. What exactly do i need to check? – Damask May 02 '13 at 02:46
  • what do you mean as cache functionality? Hibernate has a cache, not ? And what is jdb solutions ? – Damask May 02 '13 at 02:56
  • I was thinking about a second-level cache. Here is an Overview: http://www.tutorialspoint.com/hibernate/hibernate_caching.htm Sorry, I meant JDBC solution. So, for this particular page you would write a regular JDBC (by passing hibernate) and write your own SQL statement. – Rafa May 02 '13 at 20:09
  • 4
    If you have many relationships in a single Entity that is likely to result in many SELECTs before UPDATE. Are there many Cascades? If yes, try to reduce the number of cascades. Also, are there many Eager relationships? Try to move it to Lazy. – Rafa May 02 '13 at 20:12
  • 1
    I've added "lazy load" fetching to all OneToMany relations and it made application noticeably faster. Thank you. – Damask Jun 05 '13 at 05:00
  • Is it possible to avoid SELECT completely? I have a Supplier with many relationships, but when editing Supplier properties, I don't want to fetch all cascades. – Xdg Sep 11 '15 at 07:19
  • 1
    @Xdg, I don't think that is possible to avoid SELECTs completely. There are ways to reduce (e.g.: change the relationships from EAGER to LAZY). If there is a case where you need to perform multiple selects and performance is a concern; then, I would suggest moving this particular case to JDBC. – Rafa Sep 12 '15 at 18:43
  • You can use also custom update query, if you are sure that you need to persist some columns, without any condition. Have a look at https://www.baeldung.com/spring-data-partial-update#custom-query for example. Also you could read something about DynamicUpdate. – Lubo Feb 06 '23 at 16:10
0

Add @SelectBeforeUpdate(false) annotation to the entity.

Dmitry
  • 1,056
  • 1
  • 8
  • 17