What is Hibernate – Hibernate Introduction
|
|
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.
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:
- 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.
- Identity (Object Name)
- State (Object values)
- Behavior (Object Methods)
- XML
- Annotations.
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)
No. of databases we are using = That many number of configuration filesWe 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.
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> |
No comments:
Post a Comment