Hibernate, Spring, Oracle and JUnit Integration on Eclipse
Spring is mainly used to introduce loose coupling amongst different layers of application. Spring framework has enough features to provide a complete container for your application. This can be achieved by using Spring in presentation tier, middle tier and also database tier. Spring provides either its own technology, or provides support to integration with other technology in these layers. Just to explain meaning of it, Spring has Spring MVC, its own technology in presentation tier, also it can be easily integrated with Struts. In database tier, Spring supports mainly through integration with ORM technologies like Hibernate, iBatis, Toplink etc.
This article will take you through the steps required to integrate Spring and Hibernate. We change the code example in Hibernate Tutorial to integrate it with Spring. Obvious question – what all changes I need to do? Here is list of changes we require.
Additional Spring Jars
Instead of hibernate.cfg.xml, now we use applicationcontext.xml, to create connection with DB as well as define the beans to be used in application.
Code change to create Hibernate session through Spring
JUnit test class to create spring container, because our main method in CustomerDAO will not work now.
Additional Jars:
We would require following jars from Spring and JUnit.
Spring.jar
Spring-orm.jar
Spring-test.jar
Junit-4.4.jar
Changes to configuration xml.
We will not use the hibernate.cfg.xml directly, instead of that, now we are going to do it through Spring’s application context xml. In our case, the name is springhibernateintegration-appContext.xml. This will contain the definition of session factory along with other beans required to run the application. Also we want to make the session factory available in CustomerDAO, hence we create bean of CustomerDAO and inject session factory in it. We are leaving CustomerDAO to be autowired (without definition in application context xml) into CustomerDAOTest class. This is how the configuration xml will be-
springhibernateintegration-appContext.xml
oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@DB server:1521:Schema userid password
Customer.hbm.xml true thread
Make sure you make appropriate changes to the database details.
Java Changes:
We change the CustomerDAO to use the injected session factory and extend from Spring’s HibernateDaoSupport class. This is how we integrate Hibernate and Spring through Java. (Other integration we did through the configuration xml). Also, this class uses hibernate transaction instead of earlier JTA transaction. You can also configure the transaction support in xml. Second is the JUnit test class to test our code. (I am keeping this class in same folder, to avoid complexity of configuration separation). We are reading the configuration xml in test class from class path. (In case of web application, it will be integrated with web.xml).
CustomerDAO.java
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class CustomerDAO extends HibernateDaoSupport{
public Customer create(Customer newCustomer) {
Transaction transaction = this.getHibernateTemplate()
.getSessionFactory().getCurrentSession().beginTransaction();
this.getHibernateTemplate().
getSessionFactory().getCurrentSession().save(newCustomer);
transaction.commit();
return newCustomer;
}
}
?
CustomerDAOTest.java
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.test.AbstractTransactionalSpringContextTests;
public class CustomerDAOTest extends AbstractTransactionalSpringContextTests{
private CustomerDAO customerDAO;
public void setCustomerDAO(CustomerDAO aCustomerDAO) {
this.customerDAO = aCustomerDAO;
}
protected final String[] getConfigLocations() {
return new String[] {"classpath*:springhibernateintegration-appContext.xml" };
}
@Test
public void testCreate() {
Customer customer = new Customer();
customer.setCustomerName("Hibernate Spring Integration Customer");
System.out.println("Customer id before creation:" +
customer.getCustomerId());
Customer persistedCustomer = customerDAO.create(customer);
System.out.println("Customer id after creation:" +
persistedCustomer.getCustomerId());
assert(true);
}
}
This is how the Eclipse project will appear.

See Everything Working:
To see it working, run the JUnit Class as JUnit Test case through right click on it in Eclipse. Following result will be printed (just that the id may be different depending on your sequence current value).
Customer id before creation:null
Hibernate: select TAB_CUSTOMER_SEQ.nextval from dual
Hibernate: insert into TAB_CUSTOMER (CUSTOMER_NAME, CUSTOMER_ID) values (?, ?)
Customer id after creation:61









howdy, your website is really good. I thank you for deliver the results
Leave your response!