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

http://www.fileconvoy.com/dfl.php?id=g3dd6b0d0172a56609992865441cab3045564c9cba
http://www.fileconvoy.com/dfl.php?id=g2c5842d624f8f604999286545e67b178e02732f79
i changed some codes then it works properly……
1.add this to pom file
org.hibernate
hibernate-core
3.3.2.GA
org.slf4j
slf4j-log4j12
1.6.1
javassist
javassist
3.12.1.GA
2.add this in StockBoImpl
public StudentDAO getStockDao(StockDAO stockDAO) {
return stockDAO;
}
public void setStockDao(StockDao stockDao) {
this.stockDao = stockDao;
}
yep me 2
if you are just using spring+hibernate+eclipse+mysql your jars should be:
antlr-2.7.7.jar
commons-collections-3.2.1.jar
dom4j-1.6.1.jar
hibernate-3.2.3.ga.jar
spring-2.5.6.jar\spring-2.5.6.jar
commons-logging-1.1.1.jar
cglib-2.2.2.jar
asm-3.3.1.jar
jta-1.1.jar
mysql-connector-java-5.1.24.jar
Hi,
I’m getting this error when I run the app.
INFO: Building new Hibernate SessionFactory
24-Feb-2013 20:26:34 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@11121f6: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sessionFactory,stockBo,stockDao]; root of factory hierarchy
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sessionFactory’ defined in class path resource [spring/database/Hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.mkyong.common.App.main(App.java:14)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:108)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:133)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.(EntityEntityModeToTuplizerMapping.java:80)
at org.hibernate.tuple.entity.EntityMetamodel.(EntityMetamodel.java:322)
at org.hibernate.persister.entity.AbstractEntityPersister.(AbstractEntityPersister.java:485)
at org.hibernate.persister.entity.SingleTableEntityPersister.(SingleTableEntityPersister.java:133)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:286)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:814)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:732)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
… 15 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:105)
… 28 more
Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
at org.hibernate.bytecode.javassist.BytecodeProviderImpl.getProxyFactoryFactory(BytecodeProviderImpl.java:49)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(PojoEntityTuplizer.java:205)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:183)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.(AbstractEntityTuplizer.java:167)
at org.hibernate.tuple.entity.PojoEntityTuplizer.(PojoEntityTuplizer.java:77)
… 33 more
Caused by: java.lang.ClassNotFoundException: javassist.util.proxy.MethodFilter
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
… 38 more
These tutorials are pretty old and getting quite useless…how about hibernate 4, HibernateDaoSupport is not even recommended for long time now…before following these article you should look for date on it…these article are OUTDATED…
You are right Ben, here’s one Spring 3 and Hibernate 4 example http://www.cavalr.com/blog/Spring_3_and_Hibernate_4_Example.
Hope it works for you.
You should change the protostuff version the archetype pom.xml to 1.0.7 to make it work with the mvn eclipse:eclipse command.
Great article! It’d be good if you updated it to use annotations instead of hibernate xml mapping. If someone follows this article, he’ll need to modify quite many things.
Anyway, thanks!
hi, im working on this tutorial, it’s very helpfull but i have somme errors, like :
Class ‘org.apache.commons.dbcp.BasicDataSource’ not found
Class ‘org.springframework.orm.hibernate3.LocalSessionFactoryBean’ not found
HibernateDaoSupport cannot be resolved to a type > StockDaoImpl.java
and i can’t find any solution, would you like to help me please ?
Hello Mr. Yong,
Nice tutorial you’ve posted. I followed step-by-step – just got stuck in the end.
You mentioned :
————–
Import all the Spring’s beans configuration files into a single file (BeanLocations.xml), put it into the “resources/config” folder.
BeanLocations.xml
… …
Then in App.java
——————
main() method {
ApplicationContext appContext =
new ClassPathXmlApplicationContext(“spring/config/BeanLocations.xml”);
… …
———–
Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/config/BeanLocations.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/config/BeanLocations.xml] cannot be opened because it does not exist
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)
———–
I am getting above Error because of this incorrect path. tried changing to “config/…” , “/config/…” , “resources/config/…” , but all didn’t work.
Project ‘clean’ and Rebuild many times. Didn’t help much.
Also ‘target’ folder has ‘classes/resources’ – then all empty folders created for all resources needed in application. Not able to get all XML files copied under target dir.
Why would you mention “spring” in the appContext path ??
Does ‘config’ come under ‘resources/spring’ ??
Would appreciate your valuable help in resolving this. please help asap !!
Thanks, Harpreet
Hi,
Thanks for this very helpful example.
Should the line:
actually be:
Thanks,
Nana
Oops! Here is the line in the BeanLocations.xml file that I thought need to be corrected to:
Instead of…
An example on Maven + Spring MVC + Hibernate + MySQL.
That would help a lot, thanks.
Can someone pls post one example in Netbeans + Hibernate + Spring?
Hello.
I am getting the following error when trying to import the exercise file:
An internal error occurred during: “Updating Maven Project”.
Unsupported IClasspathEntry kind=4
Can anyone help me?
Group id of hibernate dependency should be “org.hibernate”. NOT just “hibernate”
here is the correct entry….
Thank you for you tips.
Thanks. This works for me.
@Charly please did you add any changes in this code because I have exception in sessionFactory Bean , thank you .
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
Hi I getting the following error and unable to resolve it plz help me…………
‘sessionFactory’ defined in class path resource [com/barun/blog/resources/spring/databases/Hibernate.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchFieldError: sqlResultSetMappings
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1403)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:545)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
javax.servlet.GenericServlet.init(GenericServlet.java:212)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:709)
I have the following error when building with maven.
Hi,could you tell us which Maven archetype because If I try use option 15 as you did I get a maven-archetype-executable archetype.
Your help is greatly appreciated
Thanks for your concise and self-explanatory tutorial! I have one small question regarding to the application design in this tutorial.
I was wondering what is the purpose of having an extra BO abstraction on top of the DAO abstraction.
Isn’t the StockDao interface already provides a good abstraction to hide the actual database operation works, which are implemented in StockDaoImpl, from the application layer?
Thanks!
Normally, Bo is for business logic, Dao is for database layer only. You can mixed both Bo and Dao together, but maintenance is hard.
Great Tutor. Thanks a million.
Hello mkyong.
Great Example. You are superb. Explained in very simple way. Very nice. Thanks a ton Guru.
I am getting this error..any help please…
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sessionFactory’ defined in class path resource [spring/database/Hibernate.xml]: Initialization of bean failed; nested exception is java.lang.StackOverflowError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.mkyong.common.App.main(App.java:14)
Caused by: java.lang.StackOverflowError
Dear Bala
run the following command on the command prompt
mvn eclipse:eclipse -Dwtpversion=1.5
By Anand singh, Napgur
Hi, which Maven archetype should I use to get a project structure like the one on the first image?
simply great
Hi Mkyonk,
Thanks for uploading the nice tutorial,
Can you please also explain me, how to generate .war file and including the JSPs in the same application.
It will be great help to me in my recent assignment..
Hi Mkyong,
thank you for all your tutorials, they inspire me,
I want to ask you, i use HibernateDaoSupport to create my Dao, and i have a many to one relation, whene itry to get the child object i have a LazyInitializationException.
i searched everywhere and i didn’t find the right solution for me.
thanks for your help
Hi,
Thanks to post such a Nice example step by step. I tried to run in my local. but i got below exception . Please help me to resolve this exception.
Hibernate annotation jar is missing? Did you include it?
Thanks For reply. I resolved problem by the help of Google.
Hi,
I am getting following error while Maven->Build option
[ERROR] Failed to execute goal on project SpringExample: Could not resolve dependencies for project com.mkyong.common:SpringExample:jar:1.0-SNAPSHOT: Failure to find javax.transaction:jta:jar:1.0.1B 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]
Please help me..
You might change the following in your pom from:
to:
Hi,
Mkyoung.
your all post is Excellent.
Thanks for this valuable content posting.
Regards,
Krunal
can someone please publish the pom.xml that can work? I am getting many NoClassFound exceptions. I use 1.6 version of java. regards.
Superb!!!! Brillant tutorila ..so easy to understand …kudos to mykong for helping us
[INFO] BUILD FAILURE
[INFO] ————————————————————————
[INFO] Total time: 2:03.485s
[INFO] Finished at: Tue Jul 24 11:31:16 ICT 2012
[INFO] Final Memory: 3M/7M
[INFO] ————————————————————————
[ERROR] Failed to execute goal on project SpringExample: Could not resolve dependencies for project com.mkyong.common:SpringExample:jar:1.0-SNAPSHOT: Could not find artifact hibernate:hibernate3:jar:3.2.3.GA in central (http://repo.maven.apache.org/maven2) -> [Help 1]
I have the same problem, how to solve it???
I am also getting same error message
Failed to execute goal on project SpringExample: Could not resolve dependencies for project com.mkyong.common:SpringExample:jar:1.0-SNAPSHOT: Could not find artifact org.hibernate:hibernate3:jar:3.2.3.GA in central (http://repo.maven.apache.org/maven2) -> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
How to solve it?
Regards
Hello MkYong, greetings to you. You contribute so much to the open source community, a true legend in my view. Amazing how u can explain advanced topics in a very simple way. Unbelievable.
Thanks for your kind words, I just try to keep it in particle n as simple as possible.
Really Thanks a lot for helping every one ….. God bless you
clear and very helpful,
thank u very much
Hello Mkyong,
Very fruitful tutorial. Thanks for uploading these tutorials!!!!!!!!!!!
Good Job!!!!!!
Is there a way to connect to remote session of hibernate?
For ex :- Instead of org.springframework.orm.hibernate3.LocalSessionFactoryBean
using org.springframework.orm.hibernate3.RemoteSessionFactoryBean
mkyong,
I am trying to follow this tutorial to get a similar system working on my machine. I am brand new to spring/hibernate but have suffcient java experience. I’m using spring version 3.1.1.RELEASE and hibernate version 4.1.1.FINAL. I believe I have made the necessary changes to this code to make it work but I am getting an error and cannot find a solution online anywhere so I thought I’d ask the expert. The error is:
SLF4J: slfjj-api 1.6.x(or later) is incompatible with this binding.
SLF4J: Your binding is version 1.5.5 or earlier (I’m using slf4j-log4j12 version 1.5.5)
SLF4j:Upgrade your binding to version 1.6.x or 2.0.x
Can you please tell me how to fix this or what changes I need to make to your code to get my versions to work!
Robi:
Here are the detailed steps to run this excellent project in Eclipse
There are some steps you need to perform before (Prepare the environment) running this in eclipse. They are:
1. Install mysql
2. Install maven
3. Download the zip file (http://www.mkyong.com/wp-content/uploads/2010/03/Spring-Hibernate-Example.zip)
4. Run “mvn clean install” (this will download the necessary/dependencies as outlined in pom.xml)
5. I ran into an error with jta.jar not being found in the repository. I added it manually thru maven
Steps for Eclipse setup:
————————
1. Steps outlined in the Preparation section
2. Extract it in your eclipse workspace directory
3. Open eclipse
4. Import this project into Eclipse (Import -> Java -> existing project)
5. Set a M2_REPO variable in eclipse and set it to the $HOME/.m2/repository
6. Now, everything in Eclipse should be set (no compile errors)
7. Right click on App.java and run as a java application
You should see the output in the console window
Hello mkyong, all your articles are so helpful to the rest of IT community. Thanks for contributing so much!
My query – you have used spring 2.5.6 using this dependency
I could see in the maven repo that spring version 3.1.1.RELEASE is also available (but that’s just for some individual spring modules such as spring-core and not for the artifact you have used i.e. spring). How can I use this version 3.1.1.RELEASE in my project? Will I have to add individual modules which are available at 3.1.1?
Since am a bigginer please let me know how to run the project in eclipse to see the exact output??