views:

307

answers:

2

In your experience what are some good Hibernate performance tweaks? I mean this in terms of Inserts/Updates and Querying.

A: 

Using caching, cascades and lazy loading appropriately.

Tweaks? Hibernate generates SQL for you, based on the mappings you give. If you don't like the SQL, then maybe Hibernate isn't the correct tool.

The rest of performance has to do with the database design: normalization, indexes, etc.

duffymo
+3  A: 

I'm not sure this is a tweak, but join fetch can be useful if you have a many-to-one that you know you're going to need. For example, if a Person can be a member of a single Department and you know you're going to need both in one particular place you can use something like from Person p left join fetch p.department and Hibernate will do a single query instead of one query for Person followed by n queries for Department.

When doing a lot of inserts/updates, call flush periodically instead of after each save or at the end - Hibernate will batch those statements and send them to the database together which will reduce network overhead.

Finally, be careful with the second level cache. If you know the majority of the objects you read by id will be in the cache, it can make things really fast, but if count on them being there but don't have it configured well, you'll end up doing a lot of single row database queries when you could have brought back a large result set with only one network/database trip.

Brian Deterling