How to reduce costs of access to relational data - part 3 - last.



Hibernate session factory enables to open sessions which represent a certain unit of work as well as beginning of a transaction. However, the correlation between the session (an object like org.hibernate.Session) and the transaction (an object like org.hibernate.Transaction) is one to many (1 -*). In particular, no transaction has to be open during the opening of the Hibernate session, which is not true for the opposite situation (then we begin the transaction after opening the session).
Until now, all the steps from 1 to 4 were connected entirely with Hibernate and will be inseparable element of an application based on Hibernate. After their formation, we begin the actual work of our application. For demonstrative purposes the class p.n.l. HibernateExample – includes two methods –
one to form a user HibernateExample.createUsers()
and the next one to display it HibernateExample.displayUsers().

After the line ‘5 . Formation of a user’ a series of calls not connected with Hibernate takes place. From the functional point of view we create a user, what influences the formation of an instance like p.n.l.User. At this point, it is possible to form any number of objects, which will finally get to a database through Hibernate. Formation and initiation of object of fields is not connected with Hibernate at all and it is held in every, chosen by the designer way. Immediately after line ‘Saving the user in a database’ there is an attempt to transfer objects to the database by means of Session.save(Object). We should remember that objects are remembered by Hibernate and are not immediately sent to the database while its deleting from cache memory of Hibernate (very important place of optimisation of a code using Hibernate).
Before we move to next steps of our examplary application, let’s have a look at the Hibernate configuration file and a file with mapping of our p.n.l.User class.
Below there is the Hibernate configuration file - hibernate.cfg.xml. There are other ways of configuring Hibernate, however, configuration through a file hibernate.cfg.xml is the most common way. The metod of finding afile by Hibernate consists of searching CLASSPATH. While building a project, we should remember to put a hibernate.cfg.xml file into an appropriate directory, which is included into CLASSPATH.

<?xml version='1.0' encoding='utf-8'?
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
<hibernate-configuration
<session-factory
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property
<property name="connection.url">jdbc:hsqldb:.</property
<property name="connection.username">sa</property
<property name="connection.password"></property
<property name="connection.pool_size">1</property
<property name="dialect">org.hibernate.dialect.HSQLDialect</property
<property name="current_session_context_class">thread</property
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property
<property name="show_sql">true</property
<property name="hbm2ddl.auto">create</property
<mapping resource="some/test/User.hbm.xml"/
</session-factory
</hibernate-configuration


Actually, meaning of given elements of a file is self explaining. Hibernate has a large set of properties, however, the file above is a good starting point to more sophisticated configuration for other projects. First of all, it should be highlighted that the structure of a hibernate.hbm.xml file is compatible with the format of XML file, so everything is between markers. Every beginning marker has its finishing counterpart and no two markers cross i.e. the finishing markers are in reverse order to the beginning ones. In the configuration file above, we use a free, relational database HSQLDB which is able to start working in embedded mode i.e. without the necessity to start a separate process with a database ( which is also possibile in HSQLDB). It simplifies testing a lot and in many cases it is also sufficient repositoryof data for building applications. show_sql and hbm2ddl.auto are important properties of Hibernate in the presented file. Their values enable to display SQL queries to a console (show_sql equals true) and to form a structure of a database while starting an application (hbm2ddl.auto equals create). After testing the application it is advisable to set the hbm2ddl.auto property on validate, simply turn it off through deleting or comment the line including a property. Another curiosity of this configuration file is indicating the file with settings of mapping class to relational representation, which will be useful while saving data by Hibernate to a database. The mapping marker is used for that and only classes included in the files and indicated by Hibernate will be used by it. It is the next, common mistake during configuring Hibernate, which can take a lot of time.
Below there is a file some/test/User.hbm.xml which was indicated by mapping marker in hibernate.cfg.xml configuration file.

<?xml version="1.0"?
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
<hibernate-mapping package="some.test"
<class name="User" table="User"
<id name="id"><generator class="native"/></id
<property name="name" column="T_NAME" length="10" not-null="true"/
<property name="surname" column="T_SURNAME" length="25" not-null="true"/
</class
</hibernate-mapping
public void displayUsers() {
// 3. Oppening of the Hibernate session
Session session = factory.openSession();
// 4. Beginning of a transaction
Transaction tx = session.beginTransaction();