Practical
We can check the hibernate behavior via different methods,
- Profiler on DAO/Session.query() methods.
- VisualVM for heap usage.
- Hibernate JMX.
- RDBMS monitoring tools.
Analyzing Hibernate
- Using properties like show_sql and format_sql.
- Log4J configurations,
- hibernate.SQL->DEBUG
- hibernate.type->TRACE
- Use P6Spy/Log4JDBC on JDBC connection.
Large Collections
- Using @BatchSize(size=100) on entities and attributes.
- Use fetch join
- Extra lazy collection fetching
- @OneToMany(fetch=FetchType.Lazy)
- @LazyCollection(LazyCollectionOption.EXTRA)
- Use “hibernate.jdbc.batch_size” / “hibernate.order_inserts” / “hibernate.order_updates”
- Use StatelessSession
- StatelessSession stateless = session.getSessionFactory().openStatelessSession();
Query Hints
- Speed up read-only service calls
- Query query = session.createQuery(“——-”);
- setReadOnly(true);
- Query Set Hints
- setHint(“org.hibernate.fetchSize”, 50);
- setHint(“org.hibernate.cacheable”, true);
- Never use second level cache.
Large Updates
- Use VERSION keyword for optimistic concurrency.
- Entities are not always necessary,
- createQuery(“UPDATE User SET active=false”).executeUpdate();
- Consider use of stored procedures.
General
- Data and schema outlive your application
- Good indexes makes huge differences
- Stored procedures are not evil
- Do not let hibernate to dictate your schema