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



<?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();


The file - User.hbm.xml shows the mapping configuration of class - some.test.
The user (attribute package of marker hibernate-mapping and attribute name of marker class). Actually, a name of the file and its location in the structure of files are not so important, however, it’s a common practice that every class which will be operated by Hibernate has its own mapping file, located in a catalog corresponding to a packet of mapping class with a name corresponding to a name of the class with the extension - hbm.xml. It is only a sort of convention which significantly speed up the process of getting accustomed by new people to the project while using Hibernate. (Obviously, a more advanced person will start getting to know persistence layer from familarising with Hibernate configuration file). The method of finding the file consists in finding the file indicated in hibernate.cfg.xml as an attribute resource of marker mapping and although the name of the file and its path do not have meaning, its location in CLASSPATH does. We have to make sure that Hibernate will be able to find the file by finding resources in CLASSPATH.
The mapping file some/test/User.hbm.xml consists of the marker class, whose attributes indicate the name of the class to which they apply (attribute name) as well as a table in the database, which will store the objects (the attribute table). Between the marker class there is actual mapping of the class fields to the corresponding fields in the database. Mapping of the fields takes place by means of markers: id and property. They are identical because of accepted attributes, however the first one – id – indicates the name of class instance variable (attribut name) that represents the key of the main table. In our example, for class instance variable named id, a generator of consecutive values was configured, which will be based on machanisms built into a chosen database.
A dialect is responsible for chosing the right mechanism, this dialect is specific for every database and it is configured in the file - hibernate.cfg.xml as a value of property - hibernate.dialect. I preceded the name of the property with a prefix hibernate even though such property does not exist in the file hibernate.cfg.xml, but only dialect does.
It is the result of using the file with format properties, in which each Hibernate property is preceded by hibernate prefix. Using it is not necessary while configuring Hibernate by the file hibernate.cfg.xml and it is only used in documentation to mark the source of property.
The attribute name of id markers and property indicate the name of class instance variable (the object world – Java), whereas column suggests the name of the column, which will store data (relational world – SQL). Latter attributes enable to express mapping more precisely and they correspond to the characteristic of the columns in the table. In our example I made use of the following attributes: column, lenght and not-null.
After saving to the database, we confirm the transaction ( line ‘7. Confirmation of the transaction’) and we close the Hibernate session ( line ‘8. Closure of the Hibernate session’). That’s how we finish the method createUsers, whose aim was to create the user of the session and its saving to the database.
The next method of our examplary application is displayUsers().

// 5. Formation a SQL query concerning a list of users to a base
Criteria criteria = session.createCriteria(User.class);
// 6. Execution of the SQL query
List users = criteria.list();
// 7. Iterating after a result of the SQL query
for (Iterator it = users.iterator(); it.hasNext();) {
User user = (User) it.next();
System.out.println(user);
}
// 8. Confirmation of the transaction
tx.commit();
// 9. Closure of the Hibernate session
session.close();
}

In the previous method we saved the information in the database, now we are going to read it. The procedure of reading data from a database begins traditionally with ‘3. Opening of Hibernate session’ and ‘4. Beginning of a transaction’. We are already familiar with this part of the program. Let’s move to a more interesting part by creating a query for downloading files. To do this we use class org.hibernate.Criteria. To create class instance we use method Session.createCriteria(Class). As a initial parameter we use the class, whose instance we want to initaite with the data from the database. The configuration of Hibernate indicates which table stores relational data representing class instance ( as a reminder: it is the part of configuration hibernate.cfg.cml marker mapping).
We can, of course, find in class o.h.Criteria methods that enables us to narrow the query down to the records we are interested in. The advantage of making a query in this way is, in the first place, transfering information concerning given fields participating in the query to an external disk (cf. some/test/User.hbm.xml), which can be easily modified without presbyopic recompilation of application.
Calling the method Criteria.list() returns the list of objects ( created on the basis of relational data) of type which is an initial parameter during creating class instance o.h.Criteria. Iterating after the results is nothing unusual and it is not connected with Hibernate at all ( apart from the fact that the implementation of the returned object of type java.util.Iterator is from Hibernate and more specialised configuration of the way we download data during asking an iterator about the next object, which will be materialised on the basis of realtional data. As I presented in the method above, the end of work with Hibernate consists of closing objects connected with it, namely: the transaction and the session (‘8. Confirmation of the transaction’ and ‘9. Closure of the Hibernate session’).

To start the application we will need the following libraries, which are provided with Hibernate ( either in the main catalog or in lib):
- hsqldb.jar
- antlr-2.7.6rc1.jar
- cglib-2.1.3.jar
- commons-collections-2.1.1.jar
- commons-logging-1.0.4.jar
- dom4j-1.6.1.jar
- hsqldb.jar
- jta.jar

After setting the environment so that the libraries are available, we start the application by calling a class: p.n.k.HibernateExample. java some.test.HibernateExample
We should remember about correct placement of the configuration files: hibernate.cfg.xml
and User.hbm.xml in the appropriate catalogs.