spring 3、hibernate 3、maven和mysql的集成
Posted
技术标签:
【中文标题】spring 3、hibernate 3、maven和mysql的集成【英文标题】:integration of spring 3, hibernate 3, maven and mysql 【发布时间】:2013-11-13 19:02:45 【问题描述】:我在 applicationContext.xml 中尝试了不同的 sessionFactory。它有同样的问题。 任何建议将不胜感激! applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Hibernate Transaction Manager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<mvc:annotation-driven />
<!-- Activates annotation based transaction management -->
<tx:annotation-driven />
<context:component-scan base-package="org.peterhuang.myweb" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></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>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.peterhuang.myweb" />
<property name="annotatedPackages" value="org.peterhuang.myweb" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
$jdbc.dialect
</prop>
<prop key="hibernate.show_sql">
$hibernate.show_sql
</prop>
<prop key="hibernate.format_sql">
$hibernate.format_sql
</prop>
</props>
</property>
</bean>
这里是 pom.xml。
<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>org.peterhuang</groupId>
<artifactId>myweb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>myweb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.6.Final</version>
</dependency>
<!-- jsp standard tag lib -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- tomcat -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<!-- jdbc mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
<build>
<finalName>myweb</finalName>
</build>
这里是错误轨迹。我在 Eclipse 中运行它,在 tomcat 7 服务器上
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'packagesToScan' of bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Bean property 'packagesToScan' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1387)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
类别DAOImpl
package org.peterhuang.myweb.dao.impl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.peterhuang.myweb.dao.CategoryDAO;
import org.peterhuang.myweb.model.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository(value = "categoryDAO")
public class CategoryDAOImpl implements CategoryDAO
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
public List<Category> getCategoryList()
// TODO Auto-generated method stub
return (List<Category>) sessionFactory.openSession()
.getNamedQuery("findAllCategory").list();
public SessionFactory getSessionFactory()
return sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
this.sessionFactory = sessionFactory;
类别DAO
package org.peterhuang.myweb.dao;
import java.util.List;
import org.springframework.stereotype.Service;
import org.peterhuang.myweb.model.*;
@Service
public interface CategoryDAO
public List<Category> getCategoryList();
Category.java
package org.peterhuang.myweb.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@NamedQueries( @NamedQuery(name = "findAllCategory", query = "from CATEGORY s ") )
@Entity
@Table(name = "CATEGORY")
public class Category
@Id
@Column(name = "CATEGORY_ID")
@GeneratedValue
private int id;
@Column(name = "CATEGORY_NAME")
private String categoryName;
public Category()
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getCategoryName()
return categoryName;
public void setCategoryName(String categoryName)
this.categoryName = categoryName;
PageController.java
package org.peterhuang.myweb.web;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.peterhuang.myweb.dao.impl.CategoryDAOImpl;
import org.peterhuang.myweb.model.Category;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PageController
@Resource
private CategoryDAOImpl categoryDAOImpl;
@RequestMapping(value = "/mainpage")
public String mainPage(Model model, HttpServletRequest request,
HttpServletResponse response)
HttpSession session = request.getSession();
List<Category> categoryList = categoryDAOImpl.getCategoryList();
System.out.println(categoryList);
session.setAttribute("categoryList", categoryList);
return "mainpage";
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>my web</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
【问题讨论】:
你能运行mvn dependency:tree
吗?让我们确保依赖项的版本符合您的预期。
我运行 mvn 测试。它建立成功
这不是我说的,但不管@Sotirios 发现了依赖问题。
【参考方案1】:
你有 JAR 地狱般的情况。
不管什么原因,这个依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.8</version>
</dependency>
有一个org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
类隐藏org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
类,即。同名,在此依赖项中
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
而且那个类没有setPackagesToScan
方法。摆脱 spring-hibernate3
依赖。 spring-orm
应该绰绰有余。你可以下载源码here自己查。
【讨论】:
我照你说的做了。错误更改为 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/applicationContext.xml] 中创建名称为“transactionManager”的 bean 时出错:bean 初始化失败;嵌套异常是 java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider; @user2788424 这是因为在 Hibernate 4 中删除了该类。正如其他人所说,您应该使用LocalSessionFactoryBean
。
@user2788424 见:***.com/questions/7528862/…
@user2788424 欢迎您。考虑支持对您有帮助的答案并接受最有用的答案。【参考方案2】:
我认为问题在于属性packagesToScan
占用String array
而不是简单的String
。尝试从以下位置更改属性:
<property name="packagesToScan" value="org.peterhuang.myweb" />
到
<property name="packagesToScan">
<list>
<value>org.peterhuang.myweb</value>
</list>
</property>
【讨论】:
您必须进行测试。 Spring 为此使用了转换服务。直接输入值没有问题。 哦。我不知道。也许OP可以尝试一下。让我们看看它是否有效。 我的意思是它不会改变任何东西。 OP 甚至在他们的评论中说。以上是关于spring 3、hibernate 3、maven和mysql的集成的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Spring 在使用 Hibernate 3 时推迟关闭 Hibernate 会话
Spring 4.1.6 和 Hibernate 3.2.2?
[转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合
延迟加载问题:Hibernate 3、Spring 4、Jackson 2.4.0