Hibernate vs. iBatis ( updated )

Hibernate and iBatis are both Database Persistence Frameworks.

Here’s the difference of both, and which to choose for appropriate usage.

Hibernate – Hibernate is seen as larger and has more functionality, and seems to be more advanced in Cache. It offers a close to Java oriented coding where there is less bother on SQL. Downside of large frameworks is the learning curve to learn something new aside from the SQL.

Hibernate workflow:

1.) Database connectivity is already encapsulated, just edit a config file and add the Database location/URL , username and password.

2.) There is a need to create an xml file and POJO ( with getters and setters ) that maps to a database table. Only one xml file and one POJO for each corresponding table.

3.) Create a Java Code, then use the hibernate methods to access the database. In a hibernate funtion, you may use SQL , HQL or none, as it depends on the method used.

Example of HQL java code:
> http://examples.javacodegeeks.com/enterprise-java/hibernate/hibernate-query-language-example/
> http://www.mkyong.com/hibernate/hibernate-query-examples-hql/

MyBatis – ( previously called iBatis ) is much more simplified and lightweight compared to Hibernate. Also best to use if there is an already existing live ( complex ) database, since it has the ability to map it easily because of its simple approach. iBatis still uses SQL and does not encapsulate it unlike Hibernate.

MyBatis workflow:

1.) Database connectivity is already encapsulated, just edit a config file and add the Database location/URL , username and password, which is quite similar to Hibernate.

2.) Create a DTO which is a POJO that similary corresponds to a database table or a structured dataset.

3.) All SQL commands and queries are written in the xml mapper file. Then each entry of a SQL set command is mapped to the DTO java file. There can be multiple mapper files and DTO files, as needed.

4.) To use the SQL commands, use the DTO java file and use the methods with the corresponding parameters as described in the xml mapper file.

Example of Mapper files.
> https://mybatis.github.io/mybatis-3/sqlmap-xml.html
> http://examples.javacodegeeks.com/enterprise-java/mybatis/how-create-mybatis-mapper/

Summary:

For a more Java and Object Oriented approach, Hibernate provides this. If comfortable to use SQL, and less on Java coding, iBatis can do it.

See more from this site:

http://www.tutorialspoint.com/ibatis/ibatis_vs_hibernate.htm

Advertisements

Struts + Spring + Hibernate Integration

Posted on 2014/07/20, Updated on 2015/08/30

Update: For new software development or building a new website from ground-up, there is better design alternative instead of using the Struts framework. Using JSTL is a better alternative, and here is a post in Webpage development using JSTL, Javascript, and JQuery. For older webpage systems, created before 2014, the Java and Struts combination is popular, and therefore a lot of legacy systems runs using these technologies.

Let’s Remember these important files that are needed in configuration of a Java Web application.
( Which includes Spring, Hibernate, and Struts )

web.xml – Servlet class to URL mapping
applicationContext.xml – Spring mapping of Bean/Object injection
struts.xml – maps the defined action to the servlet ( action class is needed in struts ) to the result jsp
hibernate.cfg.xml– database connection such as JDBC name , user/password to access the database name is defined here
<classname>.hbm.xml – the database table hibernate mapping file

index.jsp – the initial action URL, the startpage

Steps:

1.) Define org.springframework.web.context.ContextLoaderListener as the listener and org.apache.struts2.dispatcher.FilterDispatcher as the filter in web.xml

2.) Basically Spring is used here to create the predefined values, as these can be done by using the bean property injection in applicationContext.xml

Here’s a detailed explanation at this site: http://www.tutorialspoint.com/struts_2/struts_spring.htm

Webpage development in Java using Spring MVC, JSTL, Javascript and JQuery

How this website is structured in development:

1.) VIEW format – This is the front page view as shown in the browser. components: HTML and CSS for page layout, Spring MVC tags and JSTL for JSP for Java integration to Controller side.

2.) VIEW logic – Javascript and Jquery is used to create button/checkbox logic and is integrated to the HTML

3.) CONTROLLER – Built using Java classes using J2EE/HTTP libraries with Servlet Request and Response parameters. Spring annotations are also included to bind the Controller and the Models

4.) MODEL – Java classes that are POJOs ( Plain-Old-Java-Objects ) which has fields, setters and getters. Models are data storages on the transaction level. These are integrated to the JSP page and can also be used to be integrated to the Database frameworks such as Hibernate and Spring with Hibernate.

Before JSTL, the Java, Spring, Hibernate and Struts design was popular. Struts was used to bind the UI components to the Java and Spring server side code. Here is my post on Struts, Spring, and Hibernate configuration.

hibernate.cfg.xml explained

hibernate.cfg.xml files contains

1. hibenarnate.dialect – This value depends on what database you will be using. May it be DB2, MySQL, Oracle, etc..
2. hibernate.connection.driver_class – this would be the jdbc Driver Class, depending on the database used. For
MySQL it would be com.mysql.jdbc.Driver
3. hibernate.connection.url – this would be the jdbc name name and the file location of the database
4. hibernate.connection.username – Database username
5. hibernate.connection.password – Database password
6. List of all the hbm.xml files

See more here:
http://www.tutorialspoint.com/hibernate/hibernate_configuration.htm

Hibernate Advanced Concepts

Hibernate Persistence Class
– This is the Class that represents a Database table. What is required of this Class are the corresponding fields, its Setters and Getters, and a Constructor. Other properties of this table are defined in the *.hbm.xml file.

Hibernate Session
– The Session is the Object used for the database transactions. This is where we can set all our SQL ( or in this case HQL / Hibernate Query Language ) and the Persistence class for Database Transaction. It is advised only create and open a session if needed, and close it after the transaction. The Session Factory is Thread Safe, but Sessions are not.

Hibernate Batch Processing
– Hibernate allows batch processing. To do this, the session must be flush() and clear() in a certain point of the iteration, before doing transaction commit.

also another tip is to set these properties. It sort the inserts and updates, for better performance.

<prop key=”hibernate.order_inserts”>true</prop>
<prop key=”hibernate.order_updates”>true</prop>

Below are some good tips on how to do a Batch process:
http://www.tutorialspoint.com/hibernate/hibernate_batch_processing.htm
http://abramsm.wordpress.com/2008/04/23/hibernate-batch-processing-why-you-may-not-be-using-it-even-if-you-think-you-are/

Hibernate Caching
– Hibernate operates with a default first level cache, and a option to activate the secondary cache, and even additional caches from a 3rd party is permitted.

Hibernate Interceptors
– Interceptors are functions in hibernate that can do process during the time a session is doing transaction in the cache and in the database. Interceptors can cut in inside the steps of this process.

JPA and Hibernate basics

JPA is the Java Persistence API. It is the mechanism where we
can create database transactions using POJO ( Plain-Old-Java-Objects ). But however, We need to import the Java Persistence libraries called javax.persistence.*

To create a JPA we need the following:

1. entity class ( the exact database table representation )
2. persistence.xml file ( this file creates the database connection )
3. Another class where you can insert, update, or find an entity.
This class will be using the entity class.
( In this case the entity is a mirror of the database table entries )

But JPA is just a specification, ( just like REST in as described in my previous entry ) Hibernate is an implementation of JPA ( and so is several other implementations )

In Hibenarate it relates to these following files, just like how we can create JPA

1. entity class ( the exact database table representation , but no more annotations )
2. hibernate.cfg.xml – similar to the persistence.xml ( or see link )
3. *.hbm.xml – the entity configuration ( all column table’s specifications are set here )
4. classes that insert, update, and do DB transactions – You may use hibernate libraries only, or a Spring-hibernate library can be used.

NOTE: there is a certain naming convention used in hibernate in its entity class, so it must follow the setter/getter naming convetion to successfully bind to the other hibernate configurations

See here for a detailed JPA example:

http://www.javaworld.com/article/2077817/java-se/understanding-jpa-part-1-the-object-oriented-paradigm-of-data-persistence.html?page=2
http://www.javaworld.com/article/2077817/java-se/understanding-jpa-part-1-the-object-oriented-paradigm-of-data-persistence.html?page=3

How Hibernate is integrated in Spring

Hibernate can run as a stand-alone library integrated with Java.
But with Spring Framework, we can now integrate Hibernate + Spring + Java. The basic concept is that we can use the Spring Hibernate Library which already has Hibernate DAO functionalities ( DAO = Data Access Object )

In this case, Spring will do all the Hibernate session handling and database connection. In the Java Hibernate codes, we just need to configure the Java class that is the mapping of the database table and the xml database mapping must also be configured.

( I will add more to this topic since I noticed this gets hits )

In my own experience without Spring, there’s a lot of Java-Hibernate code to do, from database connection including commits and transactions. In the case where you will be using Spring, most of the repeated database transaction code can already be used under the Spring-Hibernate libraries.

Below is the table taken from the Official Spring documentation on how some parts of JDBC functions are already done in Spring ( Note that this is JDBC only, although Hibernate will use Spring JDBC functions ) :

http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/jdbc.html#jdbc-embedded-database-support

Action Spring You
Define connection parameters. X
Open the connection. X
Specify the SQL statement. X
Declare parameters and provide parameter values X
Prepare and execute the statement. X
Set up the loop to iterate through the results (if any). X
Do the work for each iteration. X
Process any exception. X
Handle transactions. X
Close the connection, statement and resultset. X

More detailed explanations in the links below:

http://howtodoinjava.com/2013/03/21/spring-3-and-hibernate-integration-tutorial-with-example/

http://www.studytrails.com/frameworks/spring/spring-hibernate-dao-support.jsp

http://www.javatpoint.com/hibernate-and-spring-integration