Friday, 15 November 2013

What is Hibernate – Hibernate Introduction








In this tutorial we will see why Hibernate came into picture though we have JDBC for connecting to the database, and what is this hibernate frame work first let us see what are the draw backs of JDBC

Draw Backs of JDBC:

  • In JDBC, if we open a database connection we need to write in try, and if any exceptions occurred catch block will takers about it, and finally used to close the connections.
  • Hear as a programmer we must close the connection, or we may get a chance to get our of connections message…!
  • Actually if we didn’t close the connection in the finally block, then jdbc doesn’t responsible to close that connection.
  • In JDBC we need to write Sql commands in various places, after the program has created if the table structure is modified then the JDBC program doesn’t work, again we need to modify and compile and re-deploy required, which is tedious.
  • JDBC used to generate database related error codes if an exception will occurs, but java programmers are unknown about this error codes right.
  • In the Enterprise applications, the data flow with in an application from class to class will be in the form of objects, but while storing data finally in a database using JDBC then that object will be converted into text.  Because JDBC doesn’t transfer objects directly.
In order to overcome above problems,  Hibernate came into picture..!

What is Hibernate:

Hibernate is the ORM tool given to transfer the data between a java (object) application and a database (Relational) in the form of the objects.  Hibernate is the open source light weight tool given by Gavin King, actually JBoss server is also created by this person only.
Hibernate is a non-invasive framework,  means it wont forces the programmers to extend/implement any class/interface, and in hibernate we have all POJO classes so its light weight.
Hibernate can runs with in or with out server, i mean it will suitable for all types of java applications (stand alone or desktop or any servlets bla bla.)
Hibernate is purely for persistence (to store/retrieve data from Database).

Mapping And Configuration Files In Hibernate







Mapping and Configuration are very familiar keywords we used to hear in the hibernate, every hibernate program must need these 2 xml files.

Mapping:

  • Mapping file is the heart of hibernate application.
  • Every ORM tool needs this mapping, mapping is the mechanism of placing an object properties into column’s of a table.
  • Mapping can be given to an ORM tool either in the form of an XML or in the form of the annotations.
  • The mapping file contains mapping from a pojo class name to a table name and pojo class variable names to table column names.
  • While writing an hibernate application, we can construct one or more mapping files, mean a hibernate application can contain any number of  mapping files.
generally an object contains 3 properties like
  • Identity (Object Name)
  • State (Object values)
  • Behavior (Object Methods)
But while storing an object into the database, we need to store only the values(State) right ? but how to avoid identity, behavior.. its not possible. In order to inform what value of an object has to be stored in what column of the table, will be taking care by the mapping,  actually mapping can be done using 2 ways,
  • XML
  • Annotations.
Actually annotations are introduced into java from JDK 1.5.

Syntax Of Mapping xml:

<hibernate-mapping> <class name="POJO class name" table="table name in database"> <id name="variable name" column="column name in database" type="java/hibernate type" /> <property name="variable1 name" column="column name in database" type="java/hibernate type" /> <property name="variable2 name" column="column name in database" type="java/hibernate type" /> </class> </hibernate-mapping>
1
2
3
4
5
6
7
8
9
<hibernate-mapping>

<class name="POJO class name" table="table name in database">
<id name="variable name" column="column name in database" type="java/hibernate type" />
<property name="variable1 name" column="column name in database" type="java/hibernate type" />
<property name="variable2 name" column="column name in database" type="java/hibernate type" />
</class>

</hibernate-mapping>


Configuration:

Configuration is the file loaded into an 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…
  • xml
  • By writing Properties file.  We don’t have annotations hear, actually in hibernate 1, 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:

<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.user">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="dialet">Database dialet class</property> <property name="hbm2ddl.auto">create/update or what ever</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>























<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.user">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="dialet">Database dialet class</property>
<property name="hbm2ddl.auto">create/update or what ever</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>


But XML files are always recommended to work.

No comments: