Choose ORM carefully!
- 2 minutes read - 334 wordsWe have recently migrated source code from Hibernate ORM to JDBC (Spring JDBC template) based implementation. Performance has been improved 10 times.
User case (Oracle 11g, JBoss 6, JDK 6, Hibernate, Spring 3) :
A tree structure in database is getting populated from a deep file system (directory structure) having around 75000 nodes. Each node (directory) contains text files, which get parsed based on business rules and then populate the database ( BRANCHs representing a node, tables referring to branch, tree_nodes). The database tables was well mapped to Hibernate JPA entries and on saving Branch object, its relevant entities were automatically getting saved. Whole operation was performed in recursive manner by traversing over directory tree. Each table’s primary key is auto generated from a separate sequence.
As per development level performance testing, it was estimated that initial tree will take 30 hours to load whole tree. This was not acceptable, as UAT cannot be started without this migration.
We have tried batching,flushing in hibernate, and seen the improvement but Spring JDBC is 10 times faster then that of Hibernate based approaches.
While migrating to Spring JDBC based implementation, the biggest issue was to resolve the generated ID referred in other queries. Since ID is getting generated by a sequence and Spring JDBC template’s ‘batchUpdate’ do not have provision to fetch generated IDs. Hibernate was doing this automatically by updating ID field of the entity object.
- We fetched bulk ids from the sequence, in a single query (select customer_id_seq.nextval from (select level from dual connect by LEVEL <=?size)) .
- Set ids in entity object while iterating BatchPreparedStatementSetter.setValues method.
This way entity object get populated in the same way as it is done in hibernate, without any special iteration/processing. Once entity object is populated, all the dependent batches can be fired so that entity.getId returns the correct value.
This way we were able to migrate Hibernate based code-base to Spring JDBC with minimal changes in source code.
This article was originally published on My LinkedIn Profile
#orm #java #hibernate #spring #technology