15 To 19 Hibernate
15 To 19 Hibernate
ORM Tool
1. 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.
2. 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-
3. JDBC used to generate database related error codes if an exception will occurs, but java programmers are
4. 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.
• Hibernate is the ORM (Object-relational mapping) 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 in 2001.
• Hibernate can runs with in or with out server, I mean it will suitable for all types of java
applications
• Hibernate has capability to generate primary keys automatically while we are storing the
records into database
• Hibernate has its own query language, i.e hibernate query language which is database
independent
• So if we change the database, then also our application will works as HQL is database
independent
• Hibernate provided Dialect classes, so we no need to write sql queries in hibernate, instead
we use the methods provided by that API.
• You know some thing.., Its saying hibernate is little slower than pure JDBC, actually the reason
being hibernate used to generate many SQL statements in run time, but I guess this is not the
disadvantage
• But there is one major disadvantage, which was boilerplate code issue, actually we need to
write same code in several files in the same application, but spring eliminated this
are very familiar keywords we used to here in the hibernate, every hibernate program must
need these 2 xml files.
• POJO is a simple java file, no need to extend any class or implement any interface.
• This POJO class contain private properties variables, and for each property a setter and a
getter.
• 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.
• Behavior (Object Methods)
• 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.
<hibernate-mapping>
</class>
</hibernate-mapping>
• 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
• 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.
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.user">user</property>
<property name="connection.password">password</property>
<!-- Related to the connection END -->
</session-factory>
</hibernate-configuration>
<hibernate-mapping>
<class name=" HibernateSample" table="STable">
<id name="stNo" column="SNo">
<generator class="assigned"/> </id>
<property name="stName" column="SName" />
<property name="stAddress "/> </class>
</hibernate-mapping>
</session-factory>
</hibernate-configuration>
CRUD.docx
Transaction tx=session.beginTransaction();
Query q=session.createQuery("update User set name=:n where id=:i");
q.setParameter("n","Udit Kumar");
q.setParameter("i",111);
int status=q.executeUpdate();
System.out.println(status);
tx.commit();
provides methods to apply criteria such as retreiving all the records of table whose salary is greater than 50000 etc.
Advantage of HCQL
• The HCQL provides methods to add criteria, so it is easy for the java programmer to add criteria. The java programmer is
• Criteria Interface
• The Criteria interface provides many methods to specify criteria. The object of Criteria can be obtained by calling the
public Criteria createCriteria(Class c)
Restrictions class provides methods that can be used to provide criteria on query.
1. lt()
2. gt()
3. le()
4. ge()
5. eq()
6. ne()