Hibernate in Action

Hibernate usage:

Session session = ...
Transaction tx = session.beginTransaction();

Pojo pojo = new Pojo();
session.save(pojo);

tx.commit();
session.close();

Interfaces:

  • Session – represents a unit of work with the database, load & save objects
  • Transaction – sets transaction boundaries
  • Configuration – object representation of hibernate configuration file, used to build a SessionFactory
    • SessionFactory factory = new Configuratio().configure().buildSessionFactory();
    • use AnnotationConfiguration subclass for annotations
    • hibernate.properties
    • hibernate.cfg.xml – defines <session-factory> element
  • SessionFactory
    • should be instantiated once at startup
    • can be shared, threadsafe
    • used to create Session objects

Logging

  • show_sql
  • format_sql
  • use_sql_comments
  • Hibernate uses Apache Commons Logging
  • log4j.logger.org.hibernate.hibernate=INFO
  • log4j.logger.org.hibernate.type=DEBUG #show JDBC bind parameters
  • Don’t log at DEBUG level in production
  • Live Statistics – sessionFactory.getStatistics()
Advertisements
Tagged with: , , ,
Posted in Hibernate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: