Hibernate File System


File System of hibernate deals with the flow of control from one file to another. There are various files that are created while using hibernate framework.

Flow of Control of Hibernate Files
1. First diagram explains the flow by taking an example. A Register.html file is made which contains the requirements of username and password. On clicking Submit button, the control goes to web.xml which is used to match the URL pattern. After that the flow goes to Servlet.java which contains the code for Hibernate explained in subsequent diagrams. Alongwith Servlet.java, another file named POJO.java is created which contains getters and setters and makes the object lightweight. The code of hibernate.cfg.xml contains the mapping resource- hibernate.hbm.xml, which is used to match the POJO objects and the database values.




2. This diagram explains the inner working of Servlet.java code. First of all, Configuration is a class that implements Serializable containing methods viz. readObject(),writeObject(), readObjectnoData(). The constructor Configuration() calls hibernate.properties jar which writes the objects created by hibernate.cfg.xml, which are dialect,URL,driver_class,username,password etc.




3. Given diagram explains that SessionFactory is an interface that implements Referenceable and Serializable and contains its respective methods.Now, buildSessionFactory method is used to execute the pre-defined values contained in hibernate.cfg.xml.


We have already learnt that hibernate is an open source object/relational mapping tool for Java. It lets us develop persistent classes. The goal of Hibernate is to relieve the programmer from 95 percent of common data persistence related programming tasks.
Following diagram tells the files which are supposed to be created or/and is created by hibernate itself:



1. First, whenever Hibernate is created, a POJO class or the constructor oriented class is created which in the above diagram is  DetailPojo.java.
2. Next, in hibernate.cfg.xml i.e. in hibernate configuration xml, mapping resource is added and is mapped to a mapping file.
3. After that, in hibernate.hbm.xml, which is the mapping file, object's name, classes, subclasses, properties and discriminator are added.
4. Finally, a servlet is made which is SignUp.java in the above example which includes SessionFactory, Transaction etc.
That is how the basic files in hibernate is created.

Downloading Hibernate
1. Considering you have latest Java version installed on your system, if you want to use Hibernate with Netbeans IDE then that can be done while installing Netbeans IDE.
2. If any other editor is to be used then, download Hibernate tools and Distributions from http://www.hibernate.org/downloads.
3. Zip files are downloaded that can later be unzipped and used as shown below.



Hibernate Prerequisites
Following are the basic list of packages/libraries that must be unstalled before using Hibernate.



Terms associated with Hibernate

1. HQL - HQL is abbreviation of Hibernate Query Language. It is SQL inspired language provided by hibernate. Developer
              can write SQL like queries to work with data objects.

2. Dialect - Each SQL vendor has its own set of syntax. This is known as dialect. In order to generate appropriate SQL query Hibernate needs to know, for which DataBase, query is to be generated. Hibernate does it by org.hibernate.dialect.Dialect class and its subclass for each vendor.
   Example:
                  DB2 : org.hibernate.dialect.DB2Dialect
                  MySQL : org.hibernate.dialect.MySQLDialect

3. Hibernate Association - Any Table in DataBase could be connected to other tables in same or other Database. And
                                         these tables could be associated with each other via some keys (primary or foreign). This kind
                                         of scenario can be handled with association.
Example: There are two tables in a Database, Student & Subject. A student can study one subject or more subjects. So, every student will have only one entry in Student table but Subject table could contain more than one values for corresponding record in Student table. Association mapping are used to map a Java Object to DataBase table's columns.

i. Entities involved in mapping:-
a)  Class (Persistence) (POJO)
b)  Database Table
c)  Mapping file (.hbm)

ii. Types of Association:-
a) Unidirectional Association:
    Many To One
    One to One
    One to Many
b) Unidirectional Association with join Table
    Many To One
    One to One
    One to Many
    Many to Many
c) Bidirectional Association
    One to Many / Many to One
    One to One
d) Bidirectional Association with join Table
    One to Many / Many to One
    One to One
    Many to Many