Performance slow at merge operation (copyValue BasicPropertyAccessor BasicSetter)

I am using hibernate 3.6.9-Final.

I use merge to persist my entity data into DB. I understand that my entity needs to be copied to the persistent entity before actual persist happens. Often times, I observe the performance of the cascadeBeforeSave function varies from time to time. Further investigation reveals that the issue happens at the BasicProperty setter, where java reflection is being used to copy the values. The setter is executed for each of my columns in the entity table, (as many as 1000 columns per table). The aggregation time of each iteration causes the user to feel the slowness of updating data.

Is this issue fixed in the latest stable version 5.x?
What factor affecting this performance. Can it be tuned?
What other alternative I have to minimize the performance variation?

Thank you.

Regards,
Khairul

First of all, it’s pretty rare to have many columns on a table so you have to understand that Hibernate is not optimized for this use case in particular. Since quite a few DBs actually also limit the amount of columns per table and resort to putting data on extra pages I can only assume the database will a certain performance issue when using that many columns as well.

Reflection optimizations are planned for Hibernate 6 but as far as I understand, this wasn’t implemented yet. Usually the JVM optimizes the reflection accesses after a while though, so in the long run you shouldn’t have very big issues due to reflection. I can only imagine the JVM has a hard time managing an object with 1000 fields though.

I would recommend to simply reduce the amount of fields/columns that you fetch by introducing a DTO projection. You can make use of the JPQL constructor syntax to fetch only the columns you actually need for this use case. This will greatly help your overall performance due to reductions in disk access, network traffic and memory usage in the JVM as well as CPU time for setting values on fields.

You might like what Blaze-Persistence Entity-Views has to offer in this regard: https://github.com/Blazebit/blaze-persistence#entity-view-usage

Thank you. If I understand you correctly. One of the keys to better performance is to limit the query to only fetch/save necessary fields/columns. I’ll have a word with my developers.

Regards,
Khairul