Monday, July 19, 2021

Hibernate - 1

1. Overview of ORM and Hibernate

2. Mappings

  1. Simple
  2. Component
  3. Inheritance 
  4. Collections
  5. Association  

3. Object states in hibernate

4. HQL

5. Criteria


JDBC - Challenges

1. Creating and Closing the connection

2. Dependency - drivers and connection related code depends on the database - whenever DB changes, code needs to be change. For any change, table rename, column added, data type.. for any small change needs application change.

3. Exception handling

4. SQL challenges - for programmer ( programmers tend to low in SQL )

5. Data conversion 


ORM - Object Relational Mapping - In simple Map Object(fields) with Table (columns)

XML based

Annotations 

Hibernate - JPA specification - lies between Java Application and JDBC

Hibernate High Level View


Creating Application

Step 1 : Dependency 

    a. hibernate

     b. JDBC

Step 2: Entity or Persistent - java bean

Step 3: Mapping - contain bean and table ( no configuration on database )

     a.  XML 

      b. Annotations

Step 4: Database (hibernate config)

a. XML

b. properties

c. Programmatic

Step 5: Client - main application 


Step 1 : use spring initializer with adding Spring data JPA,  mysql (driver depends on DB)

Step 2: Create simple Java class and decorate with annotations. @Entity and @ID are important . If you do not use @Table, @Column automatically taken name of class as table name as fields as columns


Step 5: Where should the program start ? reading the configuration for DB. From configurations create Session factory (immutable singleton).Session factory is like a pool of connections.


In Code, lets' assume you have written code to read configuration and created SessionFactory. now execute code. Will tables get created. log will show dropping and creating tables.

No tables will be created. Why ? we have not made a session and create a object and set POJO to session and execute. Then hibernate will create a table, if not ( we are running on UPDATE, not CREATE)

Still new object will not be saved. Why ? 

By default - hibernate auto commit is disable.

Create a Transaction and commit after save(). you can see the INSERT statement. 



No comments:

Post a Comment