Required Files in Hibernate Application

Required Files in Hibernate Application:
Any hibernate application must contains the files.
1. POJO class/Persistence class/Entity class/Domain class
2. Mapping File
3. Configuration File
4. Client App (One java file to write our logic(main class))
Actually these are the minimum requirement to run any hibernate application.
Whether the java application will run in the server or without server, and the application may be desktop or stand alone, swing, awt, servlet. Whatever, but the steps are common to all.
In order to work with hibernate we don’t required any server as mandatory but we need hibernate software (.jar(s) files).
No Framework is installable Software, it Means we doesn’t contain any setup.exe
When we download any framework, we will get a zip file and we need to unzip it, to get the required jar files, actually all frameworks will follow same common principles like… Framework will be in the form of a set of jar files. Every Framework software contains two types jar files.
1. Main jar files.
2. Dependent jar files
Each Framework contain one configuration file, but multiple configuration files also allowed.
We can download hibernate jar files from the following links. Based on requirement we can download the corresponding version.
For version 3.x http://sourceforge.net/projects/hibernate/files/hibernate3/
For Version 4.x http://sourceforge.net/projects/hibernate/files/hibernate4/
For Version 5.x http://sourceforge.net/projects/hibernate/files/hibernate5/
Note: Along with Hibernate jar’s we must include one more jar file, which is nothing but related to our database, this is depending on your database. For example we are working with Oracle we need to Oracle version corresponding jar for example ojdbc6.jar
1. Persistence class/Entity class/pojo class:
It should use java bean mapped with Database table Client App uses this class Object to develop (OR) mapping persistence logic.
This class Object represents database table record having synchronization b/w them.
2. Mapping file :
In this file hibernate application developer specify the mapping from entity class name to table name and entity properties names to table column names. i.e. mapping of an object oriented data to relational data is done in this file.
Standard name for this file is<domain-object-name.hbm.xml>
Mapping can be done using annotations also. If we use annotations for mapping then we no need to write mapping file.
From hibernate 3.x version on wards it provides support for annotations,So mapping can be done in two ways
XML
Annotations
Mapping is a meta data, but not the data. Through mapping meta data hibernate stores (OR) reads the data from database.
Every ORM tool needs mapping, mapping is the mechanism of placing an object properties(state) into columns of a table.
Generally an object contains 3 properties like Identity (Object Name) State (Object values) Behavior (Object Methods).
In ORM, Storing of an Object is nothing but storing the state of an Object, but not the Identity and behavior
For Example:-

An ORM tool cannot understand what class Object is needed to stored in what table. To give this information programmer must create/construct mapping.
Mapping between Employee class to Employee Table will be like


While constructing a mapping file in hibernate , It is possible to write multiple java classes mapping in a single mapping file.
In real-time project’s , for each domain object we create one mapping file
i.e, Number of Entity classes=that many number of mapping files

Syntax Of Mapping xml:
test.xml
<!DOCTYPE ………. >
<hibernate-mapping>
<class name="Fully qualified name of the class" table="database table Name ">
<id name="variable name" column="column name " type="java/hibernate type" />
<property name="variable1 name" column="column name" type="java/hibernate type" />
<property name="variable2 name" column="column name" type="java/hibernate type" />
<class>
</hibernate-mapping>
Each hibernate mapping file must contain  one <id> tag. java object identified uniquely by the <id> tag property.
<id> tag property corresponding   column can be primary key(or)non-primary key in the DataBase

Note:-
In Mapping file class names and Property names are Case-sensitive. But Table Names and Column Names are not case sensitive. When the Property name and column name both are same we no need to give Column attribute.
When the Persistence class name and table name both are same we no need to give table attribute.
In the Mapping file not required to map all the properties of the entity (pojo class) and all the columns of table.
As for our requirement we can configure required properties of the entity    with required columns of the table.
3. Configuration file:
Configuration is the file loaded into  hibernate application when working with hibernate, this configuration file contains 3 types of information.
Connection Properties
Hibernate Properties
Mapping file name(s)
We must create one configuration file for each database
we are going to use, suppose if we want to connect with 2 databases, like Oracle, MySql, then we must create 2 configuration files.
No. of databases we are using = That many number of configuration files
We can write this configuration in 2 ways…
1. xml
2. By writing Properties file. We don’t have annotations, Actually in hibernate 2.x we defined this configuration file by writing .properties file, but from 3.x  xml came into picture.
So, finally
Mapping –> xml, annotations
Configuration –> xml, .properties (old style)
Syntax Of Configuration xml:
test.xml
<hibernate-configuration>
<session-factory>
<!-- Related to the connection START -->
<property name="connection.driver_class">Driver Class Name </property>
<property name="connection.url">URL </property>
<property name="connection.username">user </property>
<property name="connection.password">password</property>
<!-- Related to the connection END -->

<!-- Related to hibernate properties START -->
<property name="show_sql">true/false</property>
<property name="dialect">Database dialect class</property>
<property name="hbm2ddl.auto">create/update/create-drop</property>
<!-- Related to hibernate properties END-->

<!-- Related to mapping START-->
<mapping resource="hbm file 1 name .xml" / >
<mapping resource="hbm file 2 name .xml" / >
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>

4. Client Application.
Find the Client application on next post.
Required Files in Hibernate Application Required Files in Hibernate Application Reviewed by Gurugubelli Technologies on December 17, 2016 Rating: 5

No comments:

Powered by Blogger.