Hibernate’s SessionFactory interface provides instances of the Session class, which represent connections to the database. Instances of SessionFactory are thread-safe and typically shared throughout an application. Session instances, on the other hand, aren’t thread-safe and should only be used for a single transaction or unit of work in an application.
The Configuration class kicks off the runtime portion of Hibernate
There are three ways to create and initialize a Configuration object.
Configuration cfg = new Configuration();
SessionFactory factory = cfg.configure().buildSessionFactory();
The configure() method tells Hibernate to load the hibernate.cfg.xml file. Without that, only hibernate.properties would be loaded from the classpath.
Configuration cfg = new Configuration();
cfg.addFile("com/manning/hq/ch03/Event.hbm.xml");
Configuration cfg = new Configuration();
cfg.addClass(com.manning.hq.ch03.Event.class);
Or you can also do this programmatically with the Configuration class:
Configuration.addJar(new java.io.File("application.jar"));
Instances of the Session class represent the primary interface to the Hibernate framework.
Persisting objects
Persisting a transient object with Hibernate is as simple as saving it with the Session instance:
Event event = new Event();
// populate the event
Session session = factory.openSession();
session.save(event);
session.flush();
Suppose you want to retrieve an Event instance from the database. If you have the Event ID, you can use a Session to return it:
Event event = (Event) session.load(Event.class, eventId);
session.close();
Notice that you’re careful to close the Session. returning the database connection to the pool.There is no need to flush the Session, since you’re not persisting objects.
This example returns a collection of all Event instances.
One easy way to improve performance within the Hibernate service, as well as your applications, is to cache objects. By caching objects in memory, Hibernate avoids the overhead of retrieving them from the database each time.
Session session = factory.openSession();
Event e = (Event) session.load(Event.class, myEventId);
e.setName("New Event Name");
session.saveOrUpdate(e);
// later, with the same Session instance
Event e = (Event) session.load(Event.class, myEventId);
e.setDuration(180);
session.saveOrUpdate(e);
session.flush();
All the updates made to the Event instance are combined into a single
update when you flush the Session.
Each object placed into the cache is keyed on the class type.
The Configuration class kicks off the runtime portion of Hibernate
There are three ways to create and initialize a Configuration object.
- (1) This first snippet loads the properties and mapping files defined in thehibernate.cfg.xml file and creates the SessionFactory:
Configuration cfg = new Configuration();
SessionFactory factory = cfg.configure().buildSessionFactory();
The configure() method tells Hibernate to load the hibernate.cfg.xml file. Without that, only hibernate.properties would be loaded from the classpath.
- (2) The Configuration class can also load mapping documents programmatically:
Configuration cfg = new Configuration();
cfg.addFile("com/manning/hq/ch03/Event.hbm.xml");
- (3) Another alternative is to have Hibernate load the mapping document based on the persistent class. The following code causes Hibernate to look for a file named com/manning/hq/Event.hbm.xml in the classpath and load the associated class:
Configuration cfg = new Configuration();
cfg.addClass(com.manning.hq.ch03.Event.class);
- (4) The hibernate.cfg.xml file supports adding all mapping files in a JAR file.
Or you can also do this programmatically with the Configuration class:
Configuration.addJar(new java.io.File("application.jar"));
Instances of the Session class represent the primary interface to the Hibernate framework.
Persisting objects
Persisting a transient object with Hibernate is as simple as saving it with the Session instance:
Event event = new Event();
// populate the event
Session session = factory.openSession();
session.save(event);
session.flush();
- Calling save(...) for the Event instance assigns a generated ID value to the instance and persists the instance. (Keep in mind that Hibernate doesn’t set the ID value if the generator type is assigned.)
- The flush() call forces persistent objects held in memory to be synchronized to the database.( Sessions don’t immediately write to the database when an object is saved. Instead, the Session queues a number of database writes to maximize performance.) which closes the JDBC connection and performs some internal cleanup.
- update(...) doesn’t assign an ID value to the object.
- saveOrUpdate(...) if use this unsaved-value attribute should be set to identify the save() state. otherwise update().
Suppose you want to retrieve an Event instance from the database. If you have the Event ID, you can use a Session to return it:
Event event = (Event) session.load(Event.class, eventId);
session.close();
Notice that you’re careful to close the Session. returning the database connection to the pool.There is no need to flush the Session, since you’re not persisting objects.
This example returns a collection of all Event instances.
Query query = session.createQuery("from Event where name = ?", "Opening Presentation");
query.setParameter(0,"OpeningPresentation", Hibernate.STRING);
List events = query.list();
The question mark in the query string represents the variable, which is similar to the JDBC PreparedStatement interface. The second method parameter is the value bound to the variable, and the third parameter tells Hibernate the type of the value. (The Hibernate class provides constants for the built-in types, such as STRING, INTEGER, and LONG, so they can be referenced programmatically.
The Session cache
query.setParameter(0,"OpeningPresentation", Hibernate.STRING);
List events = query.list();
The question mark in the query string represents the variable, which is similar to the JDBC PreparedStatement interface. The second method parameter is the value bound to the variable, and the third parameter tells Hibernate the type of the value. (The Hibernate class provides constants for the built-in types, such as STRING, INTEGER, and LONG, so they can be referenced programmatically.
The Session cache
One easy way to improve performance within the Hibernate service, as well as your applications, is to cache objects. By caching objects in memory, Hibernate avoids the overhead of retrieving them from the database each time.
Session session = factory.openSession();
Event e = (Event) session.load(Event.class, myEventId);
e.setName("New Event Name");
session.saveOrUpdate(e);
// later, with the same Session instance
Event e = (Event) session.load(Event.class, myEventId);
e.setDuration(180);
session.saveOrUpdate(e);
session.flush();
All the updates made to the Event instance are combined into a single
update when you flush the Session.
Each object placed into the cache is keyed on the class type.
No comments:
Post a Comment