, , ,

Spring 4 with Hibernate 4

July 14, 2014

Hibernate’s main feature is the Object Relational Mapping or ORM. To know more about the Hibernate click this wiki Hibernate Wiki Page and if you want to get their full documentation click this Hibernate Documentation.

This post will help you integrate your spring project with hibernate. We will use our previous project from Spring Web MVC 4 easy tutorial, I suggest you visit it before we begin this tutorial. We will also use MySQL as our database and hibernate version 4.3.5.Final.

 

1. We will need to add a couple of dependencies in our pom.xml

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
Right click the project Run As -> Maven install.
2. We need to tell the web container that our spring and hibernate is annotated. Add the following xml in your spring-mvc-servlet.xml
<context:annotation-config />
<tx:annotation-driven />

3. We will configure our sessionFactory, dataSource, transactionManager

Open your spring-mvc-servlet.xml, then add this inside the <beans> element
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean><bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/myDatabase?createDatabaseIfNotExist=true" />
<property name="username" value="root" />
<property name="password" value="" /></bean><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean><bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
4. I know you got some namespace error, open your spring-mvc-servlet.xml and add this to your namespace declaration.
xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
your beans element should look like this.
beans
5. Create a model object, we will just name it Person. We will also assign an ID for our model object.
person
After you create the Person object, you need to generate the getters and setters for all the fields.
6. We need to tell hibernate where to locate our model objects. You need to add this xml inside your bean sessionFactory, open the spring-mvc-servlet.xml and paste this.
<property name=”packagesToScan” value=”com.jabahan.hibernate” />
your sessionFactory bean should look like this.
sessionfactory
7. Now we need our DataAccessObject and its implementer.
Create an interface like this.
daointerface
Now create the implementer.
daoimpl
It’s better for you to type it rather than paste everything. LOLz!
8. We also need to tell the container that the packages used are annotated, we also need to declare the implementer of the interface.
Open your spring-mvc-servlet.xml and add this in your context component scan “com.jabahan.hibernate.dao” , and also for bean autowiring <bean id=”personDAO” class=”com.jabahan.hibernate.dao.PersonDAOImpl”/>
It should look like this.
autowire
9. Now open your HelloController.java and we will add the DAO.
hellocontroller2
10. Run your server and hit this url http://localhost:8080/HelloSpringMVC/myFirst?name=Optimus, open your database and you should see the person with name Optimus.
db
Congrats you just made your first Spring with Hibernate project.

1 comment

  1. anon - August 27, 2014 11:48 pm

    Wooohooo This is great! keep it up! jabahan na!

    Reply

Have your say