Maven + (Spring + Hibernate) Annotation + MySql Example

In last tutorial, you 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. In this tutorial, you will learn how to do the same thing in Spring and Hibernate annotation way.

Prerequisite requirement

– Installed and configured Maven, MySQL, Eclipse IDE.

The javaee.jar library is required as well, you can get it from j2ee SDK, and include it manually, there is no full version of javaee.jar available in any of the Maven repository yet.

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, Annotation and 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>
  
  <repositories>
    <repository>
      <id>JBoss repository</id>
      <url>http://repository.jboss.com/maven2/</url>
    </repository>
  </repositories>
  
  <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 annotation -->
	<dependency>
		<groupId>hibernate-annotations</groupId>
		<artifactId>hibernate-annotations</artifactId>
		<version>3.3.0.GA</version>
	</dependency>
 
	<dependency>
		<groupId>hibernate-commons-annotations</groupId>
		<artifactId>hibernate-commons-annotations</artifactId>
		<version>3.0.0.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 (Annotation)

A Stock model annotation class to store the stock data.


package com.mkyong.stock.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "stock", catalog = "mkyong", uniqueConstraints = {
		@UniqueConstraint(columnNames = "STOCK_NAME"),
		@UniqueConstraint(columnNames = "STOCK_CODE") })
public class Stock implements java.io.Serializable {

	private Integer stockId;
	private String stockCode;
	private String stockName;

	public Stock() {
	}

	public Stock(String stockCode, String stockName) {
		this.stockCode = stockCode;
		this.stockName = stockName;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "STOCK_ID", unique = true, nullable = false)
	public Integer getStockId() {
		return this.stockId;
	}

	public void setStockId(Integer stockId) {
		this.stockId = stockId;
	}

	@Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
	public String getStockCode() {
		return this.stockCode;
	}

	public void setStockCode(String stockCode) {
		this.stockCode = stockCode;
	}

	@Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
	public String getStockName() {
		return this.stockName;
	}

	public void setStockName(String stockName) {
		this.stockName = stockName;
	}

	@Override
	public String toString() {
		return "Stock [stockCode=" + stockCode + ", stockId=" + stockId
				+ ", stockName=" + stockName + "]";
	}
}
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);
}

Make this class as a bean “stockBo” in Spring Ioc container, and autowire the stock dao class.


package com.mkyong.stock.bo.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("stockBo")
public class StockBoImpl implements StockBo{
	
	@Autowired
	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. In last tutorial, you DAO classes are directly extends the “HibernateDaoSupport“, but it’s not possible to do it in annotation mode, because you have no way to auto wire the session Factory bean from your DAO class. The workaround is create a custom class (CustomHibernateDaoSupport) and extends the “HibernateDaoSupport” and auto wire the session factory, and your DAO classes extends this class.


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.util;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport
{    
    @Autowired
    public void anyMethodName(SessionFactory sessionFactory)
    {
        setSessionFactory(sessionFactory);
    }
}

package com.mkyong.stock.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

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

@Repository("stockDao")
public class StockDaoImpl extends CustomHibernateDaoSupport 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.

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. In annotation you have to use the AnnotationSessionFactoryBean, instead of LocalSessionFactoryBean, and specify your annotated model classes in ‘annotatedClasses‘ property instead of ‘mappingResources‘ property.

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.annotation.AnnotationSessionFactoryBean">

    <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="annotatedClasses">
	<list>
		<value>com.mkyong.stock.model.Stock</value>
	</list>
    </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
Import the Spring database configuration and enable the Spring’s auto scan feature.


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

	<!-- Database Configuration -->
	<import resource="../database/DataSource.xml"/>
	<import resource="../database/Hibernate.xml"/>
	
	<!-- Auto scan the components -->
	<context:component-scan 
		base-package="com.mkyong.stock" />
	
</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

Conclusion

All Spring, Hibernate related classes and configuration files are annotated, it just left the database details in XML file. Should you know how to annotate the database configuration details, please let me know. Personally, i do not use annotation feature much, because somehow you may need some workaround for certain situation, like ‘CustomHibernateDaoSupport’ extends ‘HibernateDaoSupport’ above. The mature developed XML file in Spring and Hibernate. is more preferably.

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. Read all published posts by

Comments

avatar
newest oldest most voted
anass312
Guest
anass312

Hello,

My problem is that Eclipse doesnt know import javax.persistence.Column and others imports in Class Model and annotations in class Model I dont know why, please any help.

hahoo
Guest
hahoo

org.hibernate.javax.persistence
hibernate-jpa-2.0-api
1.0.1.Final

javax.persistence
persistence-api
1.0

trackback
Spring Tutorials | Tutorials

[…] Maven + (Spring + Hibernate) Annotation + MySql Example A simple project of using Spring and Hibernate (annotation version). […]

Long
Guest
Long

what is the best way to wrap this up in a spring MVC web application?
thanks

cheskogyz
Guest
cheskogyz

Hi,

thank you for your tuto.

But i’ve got an error when trying to compile App.java :

INFO: Loading XML bean definitions from class path resource [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

I tryed but I can’t figure it out.

Can you help me?

sumethas
Guest
sumethas

And I found this log message together . Could you help me please 🙁

Caused by: java.lang.ClassNotFoundException: javax.transaction.TransactionManager
	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)
	... 23 more
10ha
Guest
10ha

Hi, I’ve been looking for the explanation between @Service @Repository,
and often I end up with saying HQL/SQL are done in @Repository (DAO)
and @Service will inject with multiple dao (repository)

do you have any example on multiple dao in @Service?
because it would give better view on how to apply multiple dao in @Service (eg. joining multiple tables, do we do this in @Service or @Repository?)

*confused*

Thanks in advance

rasev
Guest
rasev

Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [spring/config/BeanLocations.xml]; nested exception is java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/GenerationType

Parviz
Guest
Parviz

You should use javaee.jar from sun-library, because the other javaees don’t have all
necessary classes (you can compile but during run you get errors).
you can write for dependency so:

javax
javaee
1.5.0
system
C:/SUN/SDK/lib/javaee.jar

I think so you can run the app.
best
Parviz

blife
Guest
blife

Thank you very much. It helps me a lot.

ahmet kara
Guest
ahmet kara

Hi,

In the example, to insert a “Stock” object you create the instance just like below;

/** insert **/
Stock stock = new Stock();

But, isn’t it better to declare the Stock class as Spring component with the annatotion “@Component” and make the “Stock stock” decleration as @Autowired and use it like mentioned??

What is the differences or adv./disadv. between those usage??

Thanks…

malky
Guest
malky

ApplicationContext appContext =
new ClassPathXmlApplicationContext(“config/BeanLocations.xml”);

could you put some light on this statement?

Thanks

Lennart Köster
Guest
Lennart Köster

You can very well use @Autowire for the HibernateDaoSupport. Not on the setter though (it’s final) but you can use @Autowire on the constructor.

Jakob
Guest
Jakob

I get the following error: class path resource [spring/config/BeanLocations.xml] cannot be opened because it does not exist

and all i have done is to import the entire project (with the classpath file) into Eclipse. What am i doing wrong?

Vero
Guest
Vero

I put a new dependency at pom.xml

javax.persistence
persistence-api
1.0

julian
Guest
julian

I´ve installed java_ee_sdk-6u1-windows.exe but I couldn’t find javaee.jar

where could I download this file?

Bahador Biglari
Guest
Bahador Biglari

Hey, it is wonderful tutorial. thanks for your post. Could you please tell how I can run it?
Thanks
Bahador

Flavio
Guest
Flavio

Great Tutorial… but when i run it i have this error:

Exception in thread “main” java.lang.NoClassDefFoundError: Ljavax/transaction/TransactionManager;

Can you help me??

Thanks a lot

Flavio

dbzudo
Guest
dbzudo

Hi,

I tried this example with JSF, but the ‘stockBo’ is still NULL. What is wrong ?
Thanks.

@Component
@ManagedBean
@SessionScoped
public class StockBean {

@Autowired
private StockBo stockBo;
private Stock stock;

public void setStockBo(StockBo stockBo) {
this.stockBo= stockBo;
}

public void save() {
stockBo.save(stock);
}

…..

}

Sertac
Guest
Sertac

Hi, I couldnt solve the following issue 🙁 and need help please advise to get rid of this error. My POM file includes also following dependencies. javax.persistence persistence-api 1.0 javax javaee-api 6.0 ************************************************************************************ Summary of error: Unexpected exception parsing XML document from class path resource [spring/config/BeanLocations.xml]; nested exception is java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/GenerationType ************************************************************************************ Full stack trace: Connected to the target VM, address: '127.0.0.1:55489', transport: 'socket' document from class path resource [spring/config/BeanLocations.xml]; nested exception is java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in… Read more »

trackback
» Blog Archive » nice hibernate annotation tutorial
sosey
Guest
sosey

I want to use HSQL database and I changed the database:properties file.
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://localhost:8080
jdbc.username=sa
jdbc.password=

But I am taking this message

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring/config/../database/Hibernate.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:356)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:916)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:243)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:261)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:109)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1099)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:861)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:421)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
	at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:91)
	at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:75)
	at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)
	at com.mkyong.common.App.main(App.java:14)

Could you HELP me? thanks a lot…

bob
Guest
bob

Your tutorial saved my day!
Simple, clear, very helpful.
Applied on my NetBeans’ Swing + Spring 3 + Hibernate + postgresql

Many thanks for sharing!

kalam
Guest
kalam

I need one full project in spring with code and netbean supported +mysql database

Pls help me any body

ramonypp
Guest
ramonypp
BUILD FAILURE
---
Failed to read artifact descriptor for hibernate:hibernate3:jar:3.2.3.GA: Could not transfer artifact hibernate:hibernate3:pom:3.2.3.GA 
from/to JBoss repository (http://repository.jboss.com/maven2/): 
Access denied to: http://repository.jboss.com/maven2/hibernate/hibernate3/3.2.3.GA/hibernate3-3.2.3.GA.pom -> [Help 1]
AVK
Guest
AVK

Hi, I am getting following error for each downloaded example. Am I missing any configuration? org.apache.maven.archetype.old.ArchetypeTemplateProcessingException: Unable to add module to the current project as it is not of packaging type 'pom' //... [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1:14:52.281s [INFO] Finished at: Mon Jul 25 16:13:55 GMT+05:30 2011 [INFO] Final Memory: 6M/15M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2 .0:generate (default-cli) on project SpringExample: Unable to add module to the current project as it is not of packaging type 'pom' -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors,… Read more »

Mert
Guest
Mert

couldnt be more clear to understand, thank you

Chuck
Guest
Chuck

Hi, I’m new to Java world and Spring and Maven and Netbeans! (I’ve always been a Visual Studio programmer) and this is an excelent tutorial to get hooked up.

BUT… I’m trying to do a MVC Desktop Application, Model and Controller seem to work fine, but I’m having a hard time to get the data from the Controller to the View.

How can I use the Model and the Controller on the View with spring?, I have noticed that web apps use the dwr.xml to use Controller methods.

Thanks in advance!

Chuck
Guest
Chuck

I guess what im really asking is…

How to configurate spring beans configuration files to use thru different projects? In my particular case, how can I use classes from Model and Controller projects in the View project.

Or is it correct to instance a class the classic way?
Class obj = new Class();

Chuck
Guest
Chuck

Hi again mkyong,

I don’t quite get this method:

@Autowired
public void anyMethodName(SessionFactory sessionFactory)
{
setSessionFactory(sessionFactory);
}

Is it being called by someone or there’s no need? Also, the @Autowired is used to locate the SessionFactory Bean on Hibernate.xml right?

Thanks in advance.

Chuck
Guest
Chuck

The reason I’m asking this is because I want to use multiple databases and thus multiple session factories (or at leaest thats my guess so far).
Im guessing this method is being called by some delegate in some internal process but… What if I want to set a different session factory?

Cheers!

sfeher
Guest
sfeher

Hi Chuck,

Check this article:
http://www.codelark.com/tag/hibernatedaosupport/
Maybe it should help to clarify your “anyMethodName” question.

Chuck
Guest
Chuck

Ah yes! thanks sfeher!

krish
Guest
krish

Can you explain how to read datasource from tomcat server directory/external location instead of properties file

thanks
Krish

Alexander Sharma
Guest
Alexander Sharma

Hi

I have completed the tutorial I have rewritten the pom file.
I also downloaded mysql-5.5.16-win32.msi and mysql-workbench-gpl-5.2.35-win32.

pom.xml

4.0.0
com.mkyong.common
SpringExample
jar
1.0-SNAPSHOT
SpringExample
http://maven.apache.org

junit
junit
3.8.1
test

org.springframework
spring
2.5.6

cglib
cglib
2.2

mysql
mysql-connector-java
5.1.9


<!– hibernate hibernate3
3.2.3.GA –>

hibernate
hibernate-tools
3.2.3.GA

dom4j
dom4j
1.6.1

commons-logging
commons-logging
1.1.1

commons-collections
commons-collections
3.2.1

antlr
antlr
2.7.7

org.slf4j
slf4j-log4j12
1.6.2

javassist
javassist
3.4.GA
true

org.hibernate
hibernate-commons-annotations
3.2.0.Final

org.hibernate
hibernate-core
3.6.0.Final

org.glassfish.extras
javaee
3.1.1

Tsvetan
Guest
Tsvetan

Hi Mkyong,

Thank you for the great tutorial. Your blog is in my top training list. I have a small request:
Could you rewrite and post an updated “pom.xml”-file?

I read in JBoss’s blog that their repository “http://repository.jboss.com/maven2/” (which you use in the tutorial) is replaced with the new repository “https://repository.jboss.org/nexus/content/groups/”. So the current pom-file is not correct and the project does not run from the first attempt. I spent a lot of time until I find the correct dependencies. I hope that you will facilitate the other people if you update and rerun the project. Thank you in advance.

Regards,
Tsvetan

danh
Guest
danh

[ERROR] Failed to execute goal on project SpringExample: Could not resolve dependencies for project com.mkyong.common:SpringExample:jar:1.0-SNAPSHOT: Failed to collect dependencies for [junit:junit:jar:3.8.1 (test), org.springframework:spring:jar:2.5.6 (compile), cglib:cglib:jar:2.2 (compile), mysql:mysql-connector-java:jar:5.1.9 (compile), hibernate:hibernate3:jar:3.2.3.GA (compile), hibernate-annotations:hibernate-annotations:jar:3.3.0.GA (compile), hibernate-commons-annotations:hibernate-commons-annotations:jar:3.0.0.GA (compile), dom4j:dom4j:jar:1.6.1 (compile), commons-logging:commons-logging:jar:1.1.1 (compile), commons-collections:commons-collections:jar:3.2.1 (compile), antlr:antlr:jar:2.7.7 (compile)]: Failed to read artifact descriptor for hibernate:hibernate3:jar:3.2.3.GA: Could not transfer artifact hibernate:hibernate3:pom:3.2.3.GA from/to JBoss repositor
y (http://repository.jboss.com/maven2/): Access denied to: http://repository.jbo
ss.com/maven2/hibernate/hibernate3/3.2.3.GA/hibernate3-3.2.3.GA.pom -> [Help 1]
[ERROR]

I use maven 3.0.3
I got this error, please help
Thank in advance