Upload
simplepoeth
View
849
Download
2
Embed Size (px)
DESCRIPTION
Example and tutorial of integration between maven, spring,hibernate and mysql
Citation preview
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 1/15
Advertise Contact Us Write For Us RSS feed Donate
E C L I P S E H I B E R N A T E T O O L S
Java Data-object Hibernate mapping Comprehensive J2EE IDE & Support
Search
Maven + Spring + Hibernate + MySql ExamplePosted on March 23, 2010 , Last modified : August 30, 2012By mkyong
This example will use Maven to create a simple Java project structure, and demonstrate how to use Hibernate in Springframework to do the data manipulation works(insert, select, update and delete) in MySQL database.
Final project structureYour final project file structure should look exactly like following, if you get lost in the folder structure creation, please reviewthis folder structure here.
1. Table creationCreate a ‘stock’ table in MySQL database. SQL statement as follow :
Mkyong
41,545 people like Mkyong.
Facebook social plugin
Like
Recent Posts
Home All Tutorials Java Core JSF Spring Hibernate Struts Android Others
How to Apache Ant on Mac OS X
Windows 8.1, black screen with movablecursor
Java Convert String to Enum object
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 2/15
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 StructureCreate a quick project file structure with Maven command ‘mvn archetype:generate‘, see example here. Convert it toEclipse 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.commonDefine value for artifactId: : HibernateExampleDefine 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 configurationAdd 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>
Popular TutorialsEclipse Hibernate ToolsJava Dataobject Hibernate mapping Comprehensive J2EE IDE & Support
Linux How to extract a tar.gz file
Mac OSX What program is using port8080
Android Tutorial
JSF 2.0 Tutorial
Spring Tutorial
Maven Tutorial
Hibernate Tutorial
57% 53% 54%
75% 75%
84% 63% 50%
71% 75% 72%
56% 50% 42%
54% 41%
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 3/15
<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 & DAOThe Model, Business Object (BO) and Data Access Object (DAO) pattern is useful to identify the layer clearly to avoidmess up the project structure.
Stock ModelA 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))
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 4/15
Stock business object (BO) interface and implementation, it’s used to store the project’s business function, the realdatabase 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 ObjectA Stock DAO interface and implementation, the dao implementation class extends the Spring’s “HibernateDaoSupport” tomake 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;
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 5/15
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 ConfigurationCreate a ‘resources‘ folder under ‘project_name/main/java/‘, Maven will treat all files under this folder as resources file. Itwill used to store the Spring, Hibernate and others configuration file.
Hibernate ConfigurationCreate 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 ConfigurationDatabase related….
Create a properties file (database.properties) for the database details, put it into the “resources/properties” folder. It’sgood practice disparate the database details and Spring bean configuration into different files.
database.properties
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mkyongjdbc.username=rootjdbc.password=password
Create a “dataSource” bean configuration file (DataSource.xml) for your database, and import the properties fromdatabase.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"
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 6/15
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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. ThisLocalSessionFactoryBean 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/beanshttp://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/beanshttp://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>
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 7/15
<!‐‐ 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/beanshttp://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 itYou 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
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 8/15
Related PostsMaven + (Spring + Hibernate) Annotation + MySqlExample
Maven 2 + Hibernate 3.2 + MySQL Example(Annotation)
Maven 2 + Hibernate 3.2 + MySQL Example (XMLMapping)
Maven 3 + Hibernate 3.6 + Oracle 11g Example(Annotation)
Maven 3 + Hibernate 3.6 + Oracle 11g Example(XML Mapping)
Popular PostsTop 8 Java People You Should Know
Top 20 Java Websites
Top 5 Free Java eBooks
Top 10 Java Regular Expression Examples
Top 5 Open Source Q&A Systems
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
Download it – SpringHibernateExample.zip
Tags : hibernate maven mysql spring
mkyongFounder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow himon Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider makinga donation to this charity, thanks.
You might also like following tutorials :
183 Comments Mkyong.com Login
Sort by Best Share ⤤
Join the discussion…
Favorite
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 9/15
• Reply •
Harpreet • 2 years ago
see more
Hello Mr. Yong,
Nice tutorial you've posted. I followed stepbystep 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.javamain() method ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/config/BeanLocations.xml");... ...
6
• Reply •
Domício Medeiros • 10 months ago> Harpreet
On Eclipse, try to shift click on resources folder => Build Path => Use as source folder.
• Reply •
Rakesh Kumar Jha • a year ago
I got this error when running App.java :Oct 3, 2013 10:56:26 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistrydestroySingletonsINFO: Destroying singletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@13c1b02: defining beans[org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sessionFactory,stockBo,stockDao];root of factory hierarchyException in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating beanwith name 'sessionFactory' defined in class path resource [spring/database/Hibernate.xml]: Initialization ofbean failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/TransactionManageratorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)at java.security.AccessController.doPrivileged(Native Method)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)atorg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
5
• Reply •
priyanka • 10 months ago
i am getting this error..can u plz help meError: Could not find or load main class com.login.common.App
4
• Reply •
tabi • a year ago
the example is perfect and good example for those who want to setup spring hibernate quickly. there is one fix.Just replace maven dependency of hibernate3.2.3 GA with
org.hibernatehibernate3.2.3.ga
all the best cheer :) 2
Nitin Gupta • a year ago> tabi
Share ›
Share ›
Share ›
Share ›
Share ›
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 10/15
• Reply •
Nitin Gupta • a year ago> tabi
* plus + the following dependency
<dependency>
<groupid>javax.transaction</groupid>
<artifactid>jta</artifactid>
<version>1.1</version>
</dependency> 2
• Reply •
Domício Medeiros • 10 months ago> Nitin Gupta
Indeed. This dependency is missing inside pom.xml file of the example.
• Reply •
sandy • 6 months ago> Domício Medeiros
Also we need to modify .classpath file under path where you having this unziped sourcecode like as follows :
<classpathentry kind="var" path="M2_REPO/org/hibernate/hibernate/3.2.3.ga/hibernate3.2.3.ga.ja..." sourcepath="M2_REPO/org/hibernate/hibernate/3.2.3.ga/hibernate3.2.3.ga.ja..."/><classpathentry kind="var" path="M2_REPO/javax/transaction/jta/1.1/jta1.1.jar"sourcepath="M2_REPO/javax/transaction/jta/1.1/jta1.1.jar"/>
1
• Reply •
Yusuf • 2 years ago
I got maven error like this :
Failed to execute goal on project SpringExample: Could not resolve dependencies for projectcom.mkyong.common:SpringExample:jar:1.0SNAPSHOT: Failure to find hibernate:hibernate3:jar:3.2.3.GA inhttp://repo.maven.apache.org/m... was cached in the local repository, resolution will not be reattempted until theupdate interval of central has elapsed or updates are forced > [Help 1]
2
• Reply •
Bao Nguyen • 6 months ago
Thanks you so much, really helpful, I have to update pom.xml and it run perfect. 1
• Reply •
Faisal • 2 years ago
An example on Maven + Spring MVC + Hibernate + MySQL.That would help a lot, thanks.
1
• Reply •
Rajashekar • 2 months ago
Hi...i am getting error when trying add dependency of MySql. iam using MySql 5.1.73
error:Missing artifact mysql:mysqlconnectorjava:jar:5.1.7
please help me
• Reply •
Julina • 3 months ago
Great tutorial to start with Spring and hibernate together :)
• Reply •
right_now • 3 months ago
Nice tutorial! There’s a great RAD tool out called Jigy Generator that automatically spits you out a fullyconfigured spring project which can already connect to your database, authenticate users, handle file uploads,etc. It even creates dao’s, domain objects and validators in your project by reverse engineering your database.This way you don't have to get mired in the low level details of spring and hibernate... It Just Works! You candownload the project at www.getjigy.com
• Reply •
NoobMkyoung • 8 months ago
So old and not working
Sri • 10 months ago
Thanks a lot mkyong. This article is very helpful. I used these steps and tried to build my project and it worked
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 11/15
• Reply •
Thanks a lot mkyong. This article is very helpful. I used these steps and tried to build my project and it workedlike a charm. Also my project looked more organized.
• Reply •
Domício Medeiros • 10 months ago
Good tutorial, with some minnor mistakes, but a great resource of knowledge.
• Reply •
Guest • 10 months ago
This tutorial isn't worth to try. If you follow the instructions he gives, your app won't run. Wrong directories,wrong files path. Thank you for wasting my time following your howto.
• Reply •
Ayyaz Shaukat • a year ago
Good Job Man :)
• Reply •
Manish • a year ago
Failed to execute goal org.codehaus.mojo:execmavenplugin:1.2.1:exec (defaultcli) on projectSpringExample: Command execution failed. Process exited with an error: 1 (Exit value: 1) > [Help 1]
• Reply •
manish • a year ago> Manish
Pls help me in fixing this pblm. Thanks
• Reply •
Roger • a year ago
Hi, Mkyong,HibernateDaoSupport is not recommend,because this unnecessarily ties code to spring classeswhich means I have to use older version of Hibernate.So, there is no HibernateDaoSupport in packageorg.springframework.orm.hibernate4.
• Reply •
Ông Già Cô Đơn • a year ago
hi Mkyong, this example is very helpful for me. thanks so much
• Reply •
nitesh • a year ago
hi Mkyong ,your tutorial on spring+hibernate with "CRUD" operation is good but if we want to work with bulkoperations using hibernate like we were used to do in hibernate using hibernate methods and classess ,Howcan we perform in spring+hibernate scenerio ? please explain.
• Reply •
rakesh • a year ago
whe i am running in the eclipse it is showing Error exists in required project..can any one can help
• Reply •
swapnil • a year ago
Its really great article and as usual Mkyong style...great approach to develop any code base.
vinodkumar • 2 years ago
Hi ,
I tried to execute this example, It was not working in my machine due to small migration of artifact id fromhibernate3 to hibernate and group id hibernate to hibernate org.hibernate and one interdependence for hibernate3.2.7 jar i.e javax.transaction as showed below and Remote repo as
SOntatype RSASOntatype RSAhttps://oss.sonatype.org/conte...
maven2http://repo1.maven.org/maven2
org.hibernatehibernate3.2.7.ga
javax.transactionjta1.1
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 12/15
• Reply •
• Reply •
kasun • 2 years ago
http://www.fileconvoy.com/dfl....
• Reply •
kasun • 2 years ago
http://www.fileconvoy.com/dfl....
• Reply •
krish • 2 years ago
i changed some codes then it works properly......1.add this to pom file
org.hibernatehibernatecore3.3.2.GA
org.slf4jslf4jlog4j121.6.1
javassistjavassist3.12.1.GA
2.add this in StockBoImpl public StudentDAO getStockDao(StockDAO stockDAO) return stockDAO;
public void setStockDao(StockDao stockDao) this.stockDao = stockDao;
• Reply •
Saranga • 2 years ago> krish
yep me 2 1
• Reply •
Prateek Ashtikar • 8 months ago> krish
Hello, I've seen following error most of the times. Could any one please help me to resolved this error?"An internal error occurred during: "Updating Maven Project".Unsupported IClasspathEntry kind=4"
• Reply •
Kene • a year ago> krish
Thanks for your tip =)
• Reply •
Colin Hilbert • 2 years ago
if you are just using spring+hibernate+eclipse+mysql your jars should be:
antlr2.7.7.jarcommonscollections3.2.1.jardom4j1.6.1.jarhibernate3.2.3.ga.jarspring2.5.6.jar\spring2.5.6.jarcommonslogging1.1.1.jarcglib2.2.2.jarasm3.3.1.jarjta1.1.jarmysqlconnectorjava5.1.24.jar
Benjie • 2 years ago
Hi,
I'm getting this error when I run the app.
INFO: Building new Hibernate SessionFactory24Feb2013 20:26:34 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 13/15
• Reply •
see more
24Feb2013 20:26:34 org.springframework.beans.factory.support.DefaultSingletonBeanRegistrydestroySingletonsINFO: Destroying singletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@11121f6: defining beans[org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sessionFactory,stockBo,stockDao];root of factory hierarchyException in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating beanwith name 'sessionFactory' defined in class path resource [spring/database/Hibernate.xml]: Invocation of initmethod failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer[org.hibernate.tuple.entity.PojoEntityTuplizer]atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
• Reply •
Ben • 2 years ago
These tutorials are pretty old and getting quite useless...how about hibernate 4, HibernateDaoSupport is noteven recommended for long time now...before following these article you should look for date on it...these articleare OUTDATED...
• Reply •
James • 2 years ago> Ben
You are right Ben, here's one Spring 3 and Hibernate 4 example http://www.cavalr.com/blog/Spr....Hope it works for you.
• Reply •
bumbolt • 2 years ago
You should change the protostuff version the archetype pom.xml to 1.0.7 to make it work with the mvneclipse:eclipse command.
• Reply •
tkoomzaaskz • 2 years ago
Great article! It'd be good if you updated it to use annotations instead of hibernate xml mapping. If someonefollows this article, he'll need to modify quite many things.
Anyway, thanks!
• Reply •
Sana • 2 years ago
hi, im working on this tutorial, it's very helpfull but i have somme errors, like :Class 'org.apache.commons.dbcp.BasicDataSource' not foundClass '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 ?
• Reply •
Nana • 2 years ago
Hi,Thanks for this very helpful example.Should the line:
actually be:
Thanks,Nana
• Reply •
Nana • 2 years ago> Nana
Oops! Here is the line in the BeanLocations.xml file that I thought need to be corrected to:
<import resource="../spring/Stock.xml"/>
Instead of...
<import resource="../beans/Stock.xml"/>
Manish • 2 years ago
Can someone pls post one example in Netbeans + Hibernate + Spring?
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 14/15
Load more comments
• Reply •
Can someone pls post one example in Netbeans + Hibernate + Spring?
• Reply •
Karlis • 2 years ago
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?
• Reply •
PraZ • 2 years ago
Group id of hibernate dependency should be "org.hibernate". NOT just "hibernate"
here is the correct entry....
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.3.ga</version>
</dependency>
• Reply •
Charly • 2 years ago> PraZ
Thanks. This works for me.
• Reply •
Antoine • 2 years ago> Charly
@Charly please did you add any changes in this code because I have exception insessionFactory Bean , thank you .
• Reply •
snowdream • 2 years ago> PraZ
Thank you for you tips.
• Reply •
Barun • 2 years ago
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: sqlResultSetMappingsorg.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)
• Reply •
john • 2 years ago
Hi,could you tell us which Maven archetype because If I try use option 15 as you did I get a mavenarchetypeexecutable archetype.
Your help is greatly appreciated
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
Share ›
1/13/2015 Maven + Spring + Hibernate + MySql Example
http://www.mkyong.com/spring/mavenspringhibernatemysqlexample/ 15/15
Gradle – Spring MVC Web Project Example4 comments • 5 months ago
Rey — hey i just tried you example but for me its notworking...when i am running i am getting theexception WARNING: No mapping found for …
Ant – How to create a Java Project2 comments • 2 months ago
Guest — Thank you for sharing...
Spring Security + Hibernate XML Example17 comments • 8 months ago
Mohan Ram — Hi Mkyong,When enter the usernameas “mkyong” and password as “123456”. It sayusername and password incorrect.I …
Java : Return a random item from a List6 comments • 5 months ago
kpagcha — I think it would have been way moreinteresting to make performance tests instead ofshowing obvious outputs.
ALSO ON MKYONG.COM WHAT'S THIS?
Subscribe Add Disqus to your sited Privacy
All Available Tutorials
Java Core Technologies :Java I/O, Java RegEx, Java XML, Java JSON,JDBC, Java Misc
J2EE Frameworks :Hibernate, JSF 2.0, Spring Core, Spring MVC,Spring Security, Spring MongoDB, SpringBatchApache Wicket, Struts 1.x, Struts 2.x
Web Service :JAXWS (SOAP), JAXRS (REST)
Build Tools :Maven, Archiva
Unit Test Frameworks :jUnit, TestNG
Others :Android, Google App Engine, jQuery, JavaMongoDB, Quartz Scheduler, Log4j
Favorites Links
Android Getting Started
Google App Engine Java
DZone Fresh Links
Official Java EE 5 Tutorial
Official Java EE 6 Tutorial
Official Java EE 7 Tutorial
Spring 2.5.x documentation
Spring 3.2.x documentation
Spring Security 3.2.x documentation
Hibernate core 4.3 documentation
Java SE 6.0 API documentation
JSP home page
JSF home page
Eclipse IDE for Java developer
Struts 1.3.x documentation
Struts 2.3.x documentation
Maven home page
Maven central repository Search
Ant home page
JAXWS Official Website
JAXRS Official Website (Jersey)
Friends & Links
Java Code Geeks
PHP Tutorials
TenthOfMarch
Web Security Blog
Web Development
Cédric Beust (TestNG)
About Us
Mkyong.com is a weblog dedicated to Java/J2EEdevelopers and Web Developers. We constantlypublish useful tricks, tutorials on J2EE or webdevelopment.
All examples are simple, easy to read, and fullsource code available, and of course well tested inour development environment.
We're Social1. Twitter Follow Me2. Facebook Like Me3. Google Plus Add Me4. RSS Subscribe Me
Copyright © 20082014 Mkyong.com, all rights reserved.