Monday, August 13, 2018

Hibernate and Spring Integration

Now days everyone is interested in building smart application with ORM tools +Spring.Here we are going to do configuration of Hibernate with Spring.

Steps 1:Create Maven project having name as hibernate.

Steps 2:Weneed Jar file of HIbernate and Spring for integration.Add required dependencies in pom.xml file.




org.springframework
spring-jdbc
${spring.version}


cn.guoyukun.jdbc
oracle-ojdbc6
11.2.0.3.0



org.hibernate
hibernate-entitymanager
${hibernate.version}


org.springframework
spring-orm
${spring.version}


org.hibernate
hibernate-core
${hibernate.version}


org.hibernate
hibernate-ehcache
4.3.7.Final


com.github.dblock.waffle
waffle-jna
1.6


org.springframework
spring-core
${spring.version}




org.springframework
spring-context
${spring.version}


org.springframework
spring-web
${spring.version}


org.apache.httpcomponents
httpmime
${httpcomponents.httpmime}


org.springframework
spring-webmvc
${spring.version}


commons-lang
commons-lang
${commons-lang.commons-lang.version}


org.apache.httpcomponents
httpclient
${httpcomponents.httpclient}


commons-logging
commons-logging




org.daisy.libs
commons-httpclient
${commons.httpclient}


commons-logging
commons-logging




org.springframework
spring-oxm
${spring.oxm}
sources


jaxen
jaxen
${jaxen}


org.springframework.ws
spring-xml
${spring.xml}



commons-logging
commons-logging





1.1.6
4.3.5
4.3.5

3.1.0
1.7
4.0.5.RELEASE
4.3.5.Final
3.0.5.RELEASE
2.3
2.6
4.3.5
2.5
2.2.0.RELEASE

Step 3: Create Employee class and map this class with the table which you want to create .Here we have used some annotation to make our work easy.

If you are interested to find what these annotation exactly doing then please use below link for more details.

https://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/

Employee.Java class will contain below code.

package com.javatpoint;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name= "." +
"")
public class Employee {
@Id
private int id;
private String firstName,lastName;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
Step 4:Create application context file and having name as "applicationContext.xml"

a)Add datasource details in application context file







please replace value of each property e.g. replace ${db.username} with username of your database .e.g out database have user as scott so property will look like



b)we also need to setup hibernates session factory object and that will looks like

FactoryBean that creates a Hibernate SessionFactory. This is the usual way to set up a shared Hibernate SessionFactory in a Spring application context; the SessionFactory can then be passed to Hibernate-based DAOs via dependency injection.

please read below document for more details o:

http://docs.spring.io/spring-framework/docs/3.2.13.RELEASE/javadoc-api/org/springframework/orm/hibernate3/LocalSessionFactoryBean.html

your complete applicationContext.xml file will looks like.



xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">













com.javatpoint.Employee





org.hibernate.dialect.Oracle10gDialect
false
update
true
true
org.hibernate.cache.ehcache.EhCacheRegionFactory







Step 5:We are using annotation so we need to pass annotated classes so added Employee class in configuration of sessionFactory bean.Allready covered in step 4

e.g.





com.javatpoint.Employee





org.hibernate.dialect.Oracle10gDialect
false
update
true
true
org.hibernate.cache.ehcache.EhCacheRegionFactory



Step 6:For communicating with Database we need one Dao class.We will use sessionFactory in that class (EmployeeDaoImpl.java)and it will look like

package com.javatpoint.impl;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.javatpoint.Employee;
@Component
public class EmployeeDaoImpl {

@Autowired
SessionFactory sessionFactory;

public Long save(Employee t) {
try {
Long id = (Long) sessionFactory.getCurrentSession().save(t);
return id;
} catch (RuntimeException re) {
throw re;
}
}

}

Step 7:For running application will need one java class having main method.

package com.javatpoint.impl;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javatpoint.Employee;

public class InsertTest {
public static void main(String[] args) {
Employee em= new Employee();
em.setFirstName("Sagar");
em.setId(3);

ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeDaoImpl employeeDaoImpl=(EmployeeDaoImpl)context.getBean("employeeDaoImpl");
}

}

Step 8:Run main method of InsertTest class

During loading of application context session factory clss will check that whether this table is present or not in database .If table is not created sessionfactory will create table.

Note:   You can enable many hibernate properties like automatic table creation by hbm2ddl.auto ,Just add it in applciationcontext file .We have allready added them in file.so need to worry if table is not present in your databse.

 "hibernate.dialect">org.hibernate.dialect.Oracle9Dialect                 "hibernate.hbm2ddl.auto">update                  "hibernate.show_sql">true

No comments:

Post a Comment

How to check whether operating system is 64 bit or 32bit?

What is 32 and 64 bit operating system? The terms 32-bit and 64-bit refer to the way a computer's processor that is CPU, handles info...