当前位置: 首页 > 面试题库 >

Spring 3,Hibernate 3,Maven和mysql的集成

郎德馨
2023-03-14
问题内容

我在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>

问题答案:

您遇到了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应该绰绰有余。您可以在此处下载源代码并自行检查。



 类似资料:
  • 问题内容: 序言: 我在单元测试中一直在挣扎着LazyInitializationException,我很难理解它,正如我在Spring,[TestNG和Spring3中的数据库会话以及在进行Hibernate单元测试时”使用TestNG对Spring中使用的Hibernate实体类进行单元测试时,出现LazyInitializationException”遇到的LazyInitializatio

  • 问题内容: 序言:我在单元测试中一直在挣扎着LazyInitializationException,我很难理解它,正如我在Spring,TestNG和Spring 3中的Database Sessions和LazyInitializationException进行单元测试Hibernate时所看到的那样 使用TestNG在Spring中使用的实体类 为了能够清楚地提出我的问题,我在GitHub上创

  • 我正在尝试使用hibernate3 maven插件和sql maven插件。我的目标是我可以运行“maven generate sources”,它应该做到: 1) hibernate3 maven插件生成初始化。sql 2) sql maven插件执行它(一个其他脚本) 我的配置的问题是:如果我运行生成源只有hibernate3-maven-plugin它的工作原理和生成thinit.sql但我

  • 我想在我的一个项目中使用Hibernate3 maven插件。但是,因为m2e没有生命周期配置器,我需要配置生命周期插件来忽略插件的执行。但是,我也使用了m2e AFAIK不支持的maven依赖插件。 所以,我的生命周期插件的配置是: 问题是,当我在我的项目中使用Hibernate3插件时,我会在行中出现错误插件执行未被生命周期配置覆盖:org.codehaus.mojo: hibernate3-

  • 问题内容: 我已经更新到了较新版本的hibernate3-maven-plugin。我在尝试使用下面提到的插件时遇到以下错误。 感谢您解决此问题的任何指示。 问题答案: 配置方式已更改为直接使用ant hibernate工具插件。因此,配置与ant插件格式完全相同,而无需额外的taskDef例如jpaconfiguration。有关更多信息,请参见hibernate ant工具参考文档:http

  • 我正在使用hibernate3-maven-plugin版本2.0hbm2ddl从代码生成SQL脚本。我正在使用过程类。我的实体类我使用的是序列。@sequenceGenerator(Name=“sessionseq”,sequenceName=“session_seq”,allocationsize=50,initialvalue=1)当我运行Maven脚本时,这将生成create SEQUEN