Performance
Performance is a significant architectural concern. We showed how to enable statistics in Hibernate and collect various metrics via JMX in Chapter 7, Metrics and Statistics, to help you identify bottlenecks. Throughout this book, we have identified ways of improving performance. Let's revisit some of these concepts here and discuss how they are effective.
Lazy loading
The decision to use lazy loading depends on specific situations. It is important to understand how lazy loading is accomplished. As we discussed, when you load an entity, Hibernate creates and returns a proxy object, which is uninitialized. (This doesn't occur when you use the get
method.) Only when you access one of the attributes of the loaded entity does, Hibernate submit a database query to fetch the values. The same concept applies to associated entities. When you fetch the parent entities, the lazy-loaded associated entities are not fetched until they are accessed. This causes an extra trip to the database....