hibernate framework – vikramnvp11

Hi friends this is a Hibernate Framework Tutorial part 1. By the end of this post you will get link to part 2.

hibernate
hibernate

Frameworks

It is a solution for common problems which occurred in application development

Problems of JDBC

                a)Object To Value Conversion and vice versa

                b)Developer should have the knowledge of SQL

                c)Java class is directly dependent on database

                d)Developers has to manually close all the resources

Hibernate(5.x)

Hibernate is an open source ORM(Object Relational Mapping) tool

                ORM provides guildelines to map java objects directly to database without converting into values                           

Steps to create hibernate application

  • Create a simple java Project
  • Create a folder called as lib     
  • Copy all the JAR’s and do the build paths
  • Create a proper package under src
  • Make a Configuration XML file with the name “hibernate.cfg.xml” under src
  • Create a DTO class inside the package
  • Make a Mapping file
  • Create a DAO class

DTO:

DTO – A design pattern used to transfer data between the layers(MVC)

                Rules to Create a DTO class

  1.   Create a class which implements Serializable Interface
  2.   Create private data members
  3.   Generate public setters and getters
  4.   Create a public constructors
  5. The class should not be abstract

Note: MVC means

  •   Model-JDBC
  •   View-HTML
  •   Controller-Servlet

To Obtain DTD(Document Type Definition) we need to follow these steps:

  • Extract a JAR called Hibernate-core
  • Navigate to Org Folder and open hibernate folder
  • Search for a file with the name hibernate-configuration.dtd
  • Copy lines from 10 to 12 in hibernate-configuration file             

Mapping:

We Use mapping to map DTO to appropriate table. Let us see the two ways of mapping:

  • Using an XML file – XML.xml
  • By Using Annotation
    • Use @Table to provide the information about the table
    • We use @Column to map a data member to a particular column in the table
    • Use @Id to provide the information about the primary key              
    • We use @Entity to covert DTO to an entity so that hibernate can perform CRUD Operations
    • Using an XML file – XML.xml
  • By Using Annotation
    • Use @Table to provide the information about the table
    • We use @Column to map a data member to a particular column in the table
    • Use @Id to provide the information about the primary key              
    • We use @Entity to covert DTO to an entity so that hibernate can perform CRUD Operations

Steps To create a DAO class

  • Create a class with the main method
  • Create an object of DTO and populate the values
  • Use a component called as configuration
  • Load the resources(hibernate.cfg.xml and mapping Class)
  • Using Configuration obtain SessionFactory
  • Obtain a Session using SessionFactory
  • Using Session perform CRUD operations                          

There are 3 components in hibernate    

a)Configuration (Class)

b)SessionFactory (Interface)

c)Session (Interface)

a)Configuration: It is a Class which is present in org.hibernate package

We use it for 2 purpose:

                                                i)To load the resources

                                                ii)To Obtain SessionFactory Object

i)To load the resources: To Load Configuration file, we need to use configure()

  • When we use configure(), it always searches for a hibernate.cfg.xml file under src folder
  • If it doesn’t find the configuration file,it throws an exception
  • To Load mapping class, we have to use addAnnotedClass(DTO Class)

ii)To Obtain SessionFactory Object: To Obtain SessionFactory Object we have to use a buildSessionFactory(), which returns SessionFactory object,present in Configuration Object

b)SessionFactory(I): It is an Interface present in org.hibernate package. It reads a XML File and provides a Connection to the database. Using SessionFactory, we Obtain a Session Component

 c)Session(I): It is an Interface present in org.hibernate package. We use session to perform CRUD operations. To Obtain Session Object,We have to use a openSession(), present in SessionFactory Object                      

Note: An Application Should have only one SessionFactory,Sessions can be multiple      

To save an object to database

To save an object to database, we have to use save() present in Session Object and pass the reference of DTO Object as an argument. By default commit is false in hibernate(In JDBC commit is true).

Whenever we have to commit the data to db,we have to manually set the commit to true. We can do with the help of Transaction Component.

CRUD Operations

CRUD OPERATION
CRUD OPERATION

Transaction (Interface)

It is an Interface. To Obtain Transaction Object we have to use a helper method called as beginTransaction(), present in Session Object.

Hibernate has its own cache memory where save method stored.

Whenever we are manipulating the contents in the database we use
Transaction.

We use transaction for Insert, Update and delete operations 

To check the query

We have to use property tag show_sql       

To load a record from the database

We use load(DTO.class, PK),present in Session Object which returns a DTO Object

To Update a record in the table we have to follow 2 steps

a)Load the record from the database (by setting values and extract)

b)Use update()

To Delete a record in the table we have to follow 2 steps

                                a)Load the record from the database

                                b)Use delete()

To generate the database if database not exists

   createDatabaseIfNotExist=true in configuration XML file(URL)                  

To Generate the table automatically

                <property name=”hbm2ddl.auto”>create</property>

Note: When we use create Hibernate always drops the table and create new one.                                                                          

To avoid dropping of table we have to use update.

                                <property name=”hbm2ddl.auto”>update</property>

Auto Generation of Primary key

Instead Of, Developer Setting the value for primary keys, hibernate uses generators to generate the value for primary key

  • @GenericGenerator(name=”generator”,strategy=”increment”)
  • @GeneratedValue(generator=”generator”)

We can load mapping class in 2 ways

                                a)By USing addAnnotedClass()

                                b)By Using <mapping> Tag                         

There are 4 ways to load the data from the database

                                a)Using load()/Get()

                                b)SQL

                                c)HQL

                                d)Criteria                                                            

Disadvantage with load() is we cannot load multiple records from the table

Check out my other post: