Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Wednesday, 11 February 2015

Sorted collection vs Ordered collection in hibernate -

Sorted collection vs Ordered collection in hibernate  - 

Sorted Collection - 


  • A sorted collection is sorting a collection by utilizing the sorting features provided by the Java collections framework.
  •  The sorting occurs in the memory of JVM which running Hibernate, after the data being read from database using java comparator.
  • If your collection is not large, it will be more efficient way to sort it. 


Order Collection - 

  • Order collection is sorting a collection by specifying the order-by clause for sorting this collection when retrieval. 
  • If your collection is very large, it will be more efficient way to sort it 


Wednesday, 25 June 2014

Hibernate

Hibernate -

Hibernate is an open-source Java Persistence framework that lets applications connect to and work with relational databases using Object Relational Mapping . It implements the Java Persistence API - JPA which is the standard specification for object - relational mapping.Persistence is a process of storing the data to some permanent medium and retrieving it back at any point of time even after the application that had created the data ended.

 Architecture - 


Fig (a) Architecture of hibernate 

            The above diagram shows architecture of Hibernate. It creates a layer between Database and the Application. It loads the configuration details like Database connection string, entity classes, mappings etc.

Elements of the hibernate - 

SessionFactory

              The SessionFactory is a factory of session and client of ConnectionProvider. It holds second level cache (optional) of data. The org.hibernate.SessionFactory interface provides factory method to get the object of Session. 

Session

         The session object provides an interface between the application and data stored in the database. It is a short-lived object and wraps the JDBC connection. It is factory of Transaction, Query and Criteria. It holds a first-level cache (mandatory) of data. The org.hibernate.Session interface provides methods to insert, update and delete the object. It also provides factory methods for Transaction, Query and Criteria.
 

Transaction

            The transaction object specifies the atomic unit of work. It is optional. The org.hibernate.Transaction interface provides methods for transaction management.

ConnectionProvider

               It is a factory of JDBC connections. It abstracts the application from DriverManager or DataSource. It is optional.

 

TransactionFactory

               It is a factory of Transaction. It is optional.






Saturday, 21 June 2014

Object Relational Mapping - ORM

Object Relational Mapping (ORM)


        ORM is a technique for conversion of data between in-compatible type systems in object - oriented languages.Databases do not use the concept of Object Oriented languages wherein we associate objects to demonstrate the state of the systems. Database map values to primitive datatypes such as integer, real , float , String etc .Consider an example -         You have to insert an employee details into the database.If we use contemporary technique you need to convert that employee details to primitive value i.e. formulate an insert/update query with these details for that employee and then insert/update to the database.

This problem is solved by ORM .

          The Object Relational Mapping transforms any object data member type to a corresponding relational database (SQL) data source representation in any supported relational database. Relational mappings let you map an object model into a relational data model.

         Relational mappings transform object data members to relational database fields. Use them to map simple data types including primitives (such as int), JDK classes (such as String), and large object (LOB) values. You can also use them to transform object data members that reference other domain objects by way of association where data source representations require object identity maintenance (such as sequencing and back references) and possess various types of multiplicity and navigability. The appropriate mapping class is chosen primarily by the cardinality of the relationship.

Some ORM supporting APIs 

Java - 


 .Net 



Thursday, 19 June 2014

Hibernate Vs IBatis

Hibernate Vs Ibatis

There are major differences between iBatis and Hibernate but both the solutions work well, given their specific domain. Personally I would suggest you should use iBATIS if:
  • You want to create your own SQL's and are willing to maintain them.
  • Your environment is driven by relational data model.
  • You have to work on  existing and complex schema's.
  • Your environment is driven by object model and wants generates SQL automatically.
  • iBATIS is:
  • Hibernate:

And simply use Hibernate if:
You can differentiate between two as  - 
    • Simpler
    • Faster development time
    • Flexible
    • Much smaller in package size
    • Generates SQL for you which means you don't spend time on SQL
    • Provides much more advance cache
    • Highly scalable
Other difference is that iBATIS makes use of SQL which could be database dependent where as Hibernate makes use of HQL which is relatively independent of databases and it is easier to change db in Hibernate.
Hibernate maps your Java POJO objects to the Database tables where as iBatis maps the ResultSet from JDBC API to your POJO Objets.
If you are using stored procedures, well you can do it in Hibernate but it is little difficult in comparision of iBATIS. As an alternative solution iBATIS maps results sets to objects, so no need to care about table structures. This works very well for stored procedures, works very well for reporting applications, etc
Finally, Hibernate and iBATIS both are open source Object Relational Mapping(ORM) tools available in the industry. Use of each of these tools depends on the context you are using them. Hibernate and iBatis both also have good support from SPRING framework so it should not be a problem to chose one of them.