Maven + Spring + Hibernate + MySql Example

This example will use Maven to create a simple Java project structure, and demonstrate how to use Hibernate in Spring framework to do the data manipulation works(insert, select, update and delete) in MySQL database.

Final project structure

Your final project file structure should look exactly like following, if you get lost in the folder structure creation, please review this folder structure here.

1. Table creation

Create a ‘stock’ table in MySQL database. SQL statement as follow :


CREATE TABLE  `mkyong`.`stock` (
  `STOCK_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `STOCK_CODE` varchar(10) NOT NULL,
  `STOCK_NAME` varchar(20) NOT NULL,
  PRIMARY KEY (`STOCK_ID`) USING BTREE,
  UNIQUE KEY `UNI_STOCK_NAME` (`STOCK_NAME`),
  UNIQUE KEY `UNI_STOCK_ID` (`STOCK_CODE`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

2. Project File Structure

Create a quick project file structure with Maven command ‘mvn archetype:generate‘, see example here. Convert it to Eclipse project (mvn eclipse:eclipse) and import it into Eclipse IDE.


E:\workspace>mvn archetype:generate
[INFO] Scanning for projects...
...
Choose a number:  
(1/2/3....) 15: : 15
...
Define value for groupId: : com.mkyong.common
Define value for artifactId: : HibernateExample
Define value for version:  1.0-SNAPSHOT: :
Define value for package:  com.mkyong.common: : com.mkyong.common
[INFO] OldArchetype created in dir: E:\workspace\HibernateExample
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

3. Pom.xml file configuration

Add the Spring, Hibernate , MySQL and their dependency in the Maven’s pom.xml file.


<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mkyong.common</groupId>
  <artifactId>SpringExample</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SpringExample</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>

        <!-- JUnit testing framework -->
        <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
        </dependency>

        <!-- Spring framework -->
        <dependency>
	        <groupId>org.springframework</groupId>
	        <artifactId>spring</artifactId>
	        <version>2.5.6</version>
        </dependency>
    
        <!-- Spring AOP dependency -->
        <dependency>
    	        <groupId>cglib</groupId>
		<artifactId>cglib</artifactId>
		<version>2.2</version>
	</dependency>
    
        <!-- MySQL database driver -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.9</version>
	</dependency>
	
	<!-- Hibernate framework -->
	<dependency>
		<groupId>hibernate</groupId>
		<artifactId>hibernate3</artifactId>
		<version>3.2.3.GA</version>
	</dependency>
	
	
	<!-- Hibernate library dependecy start -->
	<dependency>
		<groupId>dom4j</groupId>
		<artifactId>dom4j</artifactId>
		<version>1.6.1</version>
	</dependency>
	
	<dependency>
		<groupId>commons-logging</groupId>
		<artifactId>commons-logging</artifactId>
		<version>1.1.1</version>
	</dependency>
	
	<dependency>
		<groupId>commons-collections</groupId>
		<artifactId>commons-collections</artifactId>
		<version>3.2.1</version>
	</dependency>
	
	<dependency>
		<groupId>antlr</groupId>
		<artifactId>antlr</artifactId>
		<version>2.7.7</version>
	</dependency>
	<!-- Hibernate library dependecy end -->
	
  </dependencies>
</project>

4. Model & BO & DAO

The Model, Business Object (BO) and Data Access Object (DAO) pattern is useful to identify the layer clearly to avoid mess up the project structure.

Stock Model

A Stock model class to store the stock data later.


package com.mkyong.stock.model;

import java.io.Serializable;

public class Stock implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long stockId;
	private String stockCode;
	private String stockName;

	//getter and setter methods...
}
Stock Business Object (BO))

Stock business object (BO) interface and implementation, it’s used to store the project’s business function, the real database operations (CRUD) works should not involved in this class, instead it has a DAO (StockDao) class to do it.


package com.mkyong.stock.bo;

import com.mkyong.stock.model.Stock;

public interface StockBo {
	
	void save(Stock stock);
	void update(Stock stock);
	void delete(Stock stock);
	Stock findByStockCode(String stockCode);
}

package com.mkyong.stock.bo.impl;

import com.mkyong.stock.bo.StockBo;
import com.mkyong.stock.dao.StockDao;
import com.mkyong.stock.model.Stock;

public class StockBoImpl implements StockBo{
	
	StockDao stockDao;
	
	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}

	public void save(Stock stock){
		stockDao.save(stock);
	}
	
	public void update(Stock stock){
		stockDao.update(stock);
	}
	
	public void delete(Stock stock){
		stockDao.delete(stock);
	}
	
	public Stock findByStockCode(String stockCode){
		return stockDao.findByStockCode(stockCode);
	}
}
Stock Data Access Object

A Stock DAO interface and implementation, the dao implementation class extends the Spring’s “HibernateDaoSupport” to make Hibernate support in Spring framework. Now, you can execute the Hibernate function via getHibernateTemplate().


package com.mkyong.stock.dao;

import com.mkyong.stock.model.Stock;

public interface StockDao {
	
	void save(Stock stock);
	void update(Stock stock);
	void delete(Stock stock);
	Stock findByStockCode(String stockCode);

}

package com.mkyong.stock.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.mkyong.stock.dao.StockDao;
import com.mkyong.stock.model.Stock;

public class StockDaoImpl extends HibernateDaoSupport implements StockDao{
	
	public void save(Stock stock){
		getHibernateTemplate().save(stock);
	}
	
	public void update(Stock stock){
		getHibernateTemplate().update(stock);
	}
	
	public void delete(Stock stock){
		getHibernateTemplate().delete(stock);
	}
	
	public Stock findByStockCode(String stockCode){
		List list = getHibernateTemplate().find(
                      "from Stock where stockCode=?",stockCode
                );
		return (Stock)list.get(0);
	}

}

5. Resource Configuration

Create a ‘resources‘ folder under ‘project_name/main/java/‘, Maven will treat all files under this folder as resources file. It will used to store the Spring, Hibernate and others configuration file.

Hibernate Configuration

Create a Hibernate mapping file (Stock.hbm.xml) for Stock table, put it under “resources/hibernate/” folder.


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.mkyong.stock.model.Stock" table="stock" catalog="mkyong">
        <id name="stockId" type="java.lang.Long">
            <column name="STOCK_ID" />
            <generator class="identity" />
        </id>
        <property name="stockCode" type="string">
            <column name="STOCK_CODE" length="10" not-null="true" unique="true" />
        </property>
        <property name="stockName" type="string">
            <column name="STOCK_NAME" length="20" not-null="true" unique="true" />
        </property>
    </class>
</hibernate-mapping>
Spring Configuration

Database related….

Create a properties file (database.properties) for the database details, put it into the “resources/properties” folder. It’s good practice disparate the database details and Spring bean configuration into different files.

database.properties


jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mkyong
jdbc.username=root
jdbc.password=password

Create a “dataSource” bean configuration file (DataSource.xml) for your database, and import the properties from database.properties, put it into the “resources/database” folder.

DataSource.xml


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	
<bean 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="location">
		<value>properties/database.properties</value>
	</property>
</bean>

<bean id="dataSource" 
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="${jdbc.driverClassName}" />
	<property name="url" value="${jdbc.url}" />
	<property name="username" value="${jdbc.username}" />
	<property name="password" value="${jdbc.password}" />
</bean>

</beans>

Hibernate related….

Create a session factory bean configuration file (Hibernate.xml), put it into the “resources/database” folder. This LocalSessionFactoryBean class will set up a shared Hibernate SessionFactory in a Spring application context.

Hibernate.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
<!-- Hibernate session factory -->
<bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
     </property>
    	
     <property name="mappingResources">
	<list>
           <value>/hibernate/Stock.hbm.xml</value>
	</list>
      </property>	

    </bean>
</beans>    

Spring beans related….

Create a bean configuration file (Stock.xml) for BO and DAO classes, put it into the “resources/spring” folder. Dependency inject the dao (stockDao) bean into the bo (stockBo) bean; sessionFactory bean into the stockDao.

Stock.xml


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- Stock business object -->
   <bean id="stockBo" class="com.mkyong.stock.bo.impl.StockBoImpl" >
   		<property name="stockDao" ref="stockDao" />
   </bean>
 
   <!-- Stock Data Access Object -->
   <bean id="stockDao" class="com.mkyong.stock.dao.impl.StockDaoImpl" >
   		<property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   
</beans>

Import all the Spring’s beans configuration files into a single file (BeanLocations.xml), put it into the “resources/config” folder.

BeanLocations.xml


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<!-- Database Configuration -->
	<import resource="../database/DataSource.xml"/>
	<import resource="../database/Hibernate.xml"/>
	
	<!-- Beans Declaration -->
	<import resource="../beans/Stock.xml"/>
	
</beans>

6. Run it

You have all the files and configurations , run it.


package com.mkyong.common;

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

import com.mkyong.stock.bo.StockBo;
import com.mkyong.stock.model.Stock;

public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext appContext = 
    	  new ClassPathXmlApplicationContext("spring/config/BeanLocations.xml");
	
    	StockBo stockBo = (StockBo)appContext.getBean("stockBo");
    	
    	/** insert **/
    	Stock stock = new Stock();
    	stock.setStockCode("7668");
    	stock.setStockName("HAIO");
    	stockBo.save(stock);
    	
    	/** select **/
    	Stock stock2 = stockBo.findByStockCode("7668");
    	System.out.println(stock2);
    	
    	/** update **/
    	stock2.setStockName("HAIO-1");
    	stockBo.update(stock2);
    	
    	/** delete **/
    	stockBo.delete(stock2);
    	
    	System.out.println("Done");
    }
}

output


Hibernate: insert into mkyong.stock (STOCK_CODE, STOCK_NAME) values (?, ?)
Hibernate: select stock0_.STOCK_ID as STOCK1_0_, 
stock0_.STOCK_CODE as STOCK2_0_, stock0_.STOCK_NAME as STOCK3_0_ 
from mkyong.stock stock0_ where stock0_.STOCK_CODE=?
Stock [stockCode=7668, stockId=11, stockName=HAIO]
Hibernate: update mkyong.stock set STOCK_CODE=?, STOCK_NAME=? where STOCK_ID=?
Hibernate: delete from mkyong.stock where STOCK_ID=?
Done

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
126 Comment threads
79 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
140 Comment authors
SaviraAshish DwivediPriyaRahulsarat Recent comment authors
newest oldest most voted
Rahul
Guest
Rahul

[ERROR] Failed to execute goal on project SpringExample: Could not resolve dependencies for project com.mkyong.common:SpringExample:jar:1.0: Failure to find org.hibernate:hibernate:jar:3.2.3.ga in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]

Priya
Guest
Priya

Same error.

priyanka
Guest
priyanka

i am getting this error..can u plz help me
Error: Could not find or load main class com.login.common.App

sarat
Guest
sarat

problem with your package …

anusha
Guest
anusha

HibernateDaoSupport is not present in Hibernate dependency you provide it throws an error needed javax-transaction jar
could you please suggest any changes in the pom.xml
After searching I replaced hibernate dependency with spring-orm and I have still problems
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration

sarat
Guest
sarat

change dependency to

org.hibernate
hibernate
3.2.3.ga

then clean and re install the project if ur using eclipse or netbeans
it may help ..

hemant
Guest
hemant

in your project directory every xml file in different folder not in resource]

John Ryan
Guest
John Ryan

Very usefull, many thanks.

trackback
Maven + (Spring + Hibernate) Annotation + MySql Example | Spring

[…] Annotation + MySql Example Written on March 31, 2010 at 3:12 pm by mkyong In last tutorial, you use Maven to create a simple Java project structure, and demonstrate how to use Hibernate in […]

trackback
Hibernate Tutorials | Tutorials

[…] Spring + Hibernate Integration Example to integrate Hibernate with Spring framework. […]

mignight_blue
Guest
mignight_blue

Very well explained and easy to follow. Thanks.

jb
Guest
jb

I get the following exception when I run App.java, form both the command line and within eclipse.

Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:169)
	... 25 more
Elvis
Guest
Elvis

Jb would’ve download with maven this dependency

javax.transaction
jta
1.1

Alejandro Flowers
Guest
Alejandro Flowers

Hi, I download you example but I get the following exception when I run the project with tomcat:

Caused by: java.lang.NoSuchMethodError: 
org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;
[Ljava/lang/String;Ljava/lang/String;)V

Have you any idea??? can you help me????

Bill
Guest
Bill

I get the following message when I run the App.java

Error creating bean with name ‘sessionFactory’ defined in class path resource [spring/database/Hibernate.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

I want to solve this problem. Thanks

jk
Guest
jk

I get the following exception when I run App.java, form both the command line and within eclipse.

Exception in thread “main” org.springframework.dao.DataAccessResourceFailureException: Cannot open connection; nested exception is org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:627)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
at com.pm.user.UserDaoImp.save(UserDaoImp.java:12)
at com.pm.app.app.main(app.java:26)

jk
Guest
jk

oh, I solve it!
I always appreciate many useful resources ๐Ÿ˜€

jorge
Guest
jorge

And how did you solve it ?

Jessica
Guest
Jessica

Hi Guys!
when I type $mvn test I get:

1 required artifact is missing.
for artifact:
com.mkyong.common:SpringExample:jar:1.0-SNAPSHOT

Can you help me?

SpringMvcBeginner
Guest
SpringMvcBeginner

how will I know if Maven, MySQL and Eclipse IDE are installed and configured properly? Thanks

Sunil Verma
Guest
Sunil Verma

well done.Thanks a ton.
What different design patterns were followed to build this sample project?

toto
Guest
toto

Following jars added in /WEB-INF/lib

1)hibernate3.jar
2)jta.jar
3)jaxen-1.1-beta-4.jar
4)ehcache-1.1.jar

Hamid
Guest
Hamid

Hello,

I have a problem with this example !! when I added the dependencies throw maven, maven didn’t update the eclipse classpath, so when I try to use HibernateDaoSupport i got erreur ??
Do you have any idea how can I solve this issue ??

best regards,

Abdul
Guest
Abdul

Very easy to follow and to undestand. Thanks

Abdul
Guest
Abdul

easy to follow and to understand. thanks

Xavier Balloy
Guest
Xavier Balloy

Hi,
I’ve got a database with a lot of table. Is there a way to generate Model, DO and DAO ?
Thanks!

Bahador Biglari
Guest
Bahador Biglari

This is the error that I get once I run it. Please help me to figure it out. I am waiting for your help.
Thanks Bahador

Caused by: java.io.FileNotFoundException: class path resource [spring/config/BeanLocations.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:143)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
… 13 more

Bahador Biglari
Guest
Bahador Biglari

I add the resources to the class path and everything goes well. Thanks for your wonderful post, Well Done !!!!

Ramesh.kunchapu
Guest
Ramesh.kunchapu

Easy to understand , tnks

j
Guest
j

Missing artifact hibernate:hibernate3:jar:3.2.3.GA:compile

raca
Guest
raca

I have the same error !
The following artifacts could not be resolved: hibernate:hibernate3:jar:3.2.3.GA

Tsvetan
Guest
Tsvetan

Hi,
Mkyong is rigth. Try this:

org.hibernate
hibernate
3.2.3.ga

Regards,
Tsvetan

Gaurav
Guest
Gaurav

Hi ๐Ÿ™‚
You are right

Thanx

Javier
Guest
Javier

Hi mkyong! before anything let me say you’re amazing! all of your articles are easy to understand and follow!

Now let me ask you a question, what view of eclipse do you use? Java or JavaEE?
Which of them you recomend me?

Thanks a lot!

nick
Guest
nick

I am getting the following error, would appreciate your help

Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sessionFactory’ defined in class path resource [resources/database/Hibernate.xml]: Invocation of init method failed; nested exception is java.lang.IllegalAccessError

juan
Guest
juan

Excellent tutorial! really thank you.

One question.

I use maven and i have slf4j in my maven dependencys on the project.
But i still get this error

Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sessionFactory’ defined in class path resource [spring/database/Hibernate.xml]:

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:115)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877)
… 16 more

any ideas?

juan
Guest
juan

I think the solution is to add this on the POM xml org.slf4j slf4j-log4j12 1.6.1 org.slf4j slf4j-api 1.6.1 javassist javassist 3.4.GA true Its work for me!

Pancho Lopez
Guest
Pancho Lopez

What if i have no POM.xml file? =O

sarovitch
Guest
sarovitch

Salut Juan
I have the same probleme.
in the “javassist” dependency to add, what the taglib of the “true” ?
think you

juan
Guest
juan

Ok, i solved it. I have to add in my pom.xml this dependencys to get this thing work! org.slf4j slf4j-log4j12 1.6.1 org.slf4j slf4j-api 1.6.1 javassist javassist 3.4.GA true If i am correct, please change the pom.xml tutorial so other people dont get this error! Thanks!

RASKA
Guest
RASKA

Hey Mkyong, these are the best tutorials I’ve ever read it. Your tutorials for dummies are so cool and so easy to understand.

Thanks a lot.
RASKA.