4

We use Hibernate as our ORM layer on top of a MySQL database. We have quite a few model objects, of which some are quite large (in terms of number of fields etc.). Some of our queries requires that a lot (if not all) of the model objects are retrieved from the database, to do various calculations on them.

We have lazy loading enabled, but in some cases it still takes a significant amount of time for Hibernate to populate the objects. The execution time of the MySQL query is very fast (in the order of a few milliseconds), but then Hibernate takes its sweet time to populate the objects.

Is there any way / pattern / optimization to speed up this process?

Thanks.

Nico Huysamen
  • 10,217
  • 9
  • 62
  • 88

3 Answers3

2

One approach is to not populate the entity but some kind of view object.

Assuming a CustomerView has the appropriate constructor, you can do

select new CustomerView(c.firstname, c.lastname, c.age) from Customer c

Though I'm a bit surprised about Hibernate being slow to populate objects unless you happen to load associated objects by cascade and forget a few appropriate fetches.

  • Creating views not really an option at the moment. We have a large amount of models and no time for such an intense refactoring process. Was hoping for a quick-win. But thanks, definitely something to keep in mind for the future. – Nico Huysamen Jun 03 '11 at 09:44
1

Perhaps consider adding a second level cache? This won't necessarily speed up the object instantiation, but it could considerably cut down the frequency in which you are needing to do that.

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html

Steve Siebert
  • 1,874
  • 12
  • 18
1

Since you're asking a performance-related question, you might want to collect more data on where the bottleneck is. You say

Hibernate takes its sweet time to populate the objects.

How do you know it's Hibernate that's the problem? In other words, is Hibernate itself the problem, or could there not be enough memory (or too much) so the JVM isn't running efficiently?

Also, you mention

We have quite a few model objects, of which some are quite large (in terms of number of fields etc.).

How many is "quite large"? Dozens? Hundreds? Thousands? It makes a big difference, because relational databases (such as MySQL) start performing more poorly as your table gets "wider" (see this question: Is there a performance decrease if there are too many columns in a table?).

Performance is a lot about balancing constraints, but it's also about collecting a lot of data to see where the problem is and then fixing that problem. Then you'll find the next bottleneck and fix that one until your performance is good enough, or you run out of implementation time.

Community
  • 1
  • 1
Ted M. Young
  • 1,052
  • 11
  • 24
  • 1) Yes, it is Hibernate taking it's time creating the objects from the db. I ran it through a profiler, and also if I select explicit fields (instead of objects) it works fine. 2) I actually don't work at the company anymore, but if I remember correctly it was in the order of hundreds. I don't think any of them had more fields than about 30 to 40. About 90% only had about 10. – Nico Huysamen Jul 31 '11 at 07:31
  • I'm quite surprised that Hibernate is taking so long to create the objects (of course, you haven't mentioned exactly how long "a significant amount of time" is?). Is it in the instantiation of the objects, or in waiting for the results from the database, or populating the fields of the objects? I've had systems with larger objects and they don't take very much time at all. But I guess you're no longer there, so never mind. :-) – Ted M. Young Aug 07 '11 at 03:04