简体   繁体   English

Spring MVC和Hibernate集成

[英]Spring MVC and Hibernate integration

I am having problems integrating Spring MVC and Hibernate. 我在集成Spring MVC和Hibernate时遇到问题。

I assume I am missing some configuration to tell Spring to use Hibernate, because there aren't any errors when running the application, yet the database is not updated with my entities. 我假设我缺少一些配置来告诉Spring使用Hibernate,因为在运行应用程序时没有任何错误,但是数据库并未使用我的实体进行更新。

In main/webapp/WEB-INF , I have 3 configuration files: main/webapp/WEB-INF ,我有3个配置文件:

web.xml web.xml中

<web-app 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>Spring MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/mvc-dispatcher-servlet.xml,
            /WEB-INF/spring-configuration.xml
        </param-value>
    </context-param>
</web-app>

mvc-dispatcher-servlet.xml MVC-调度-servlet.xml中

<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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="org.mypackage.controller"/>
    <context:annotation-config/>
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

spring-configuration.xml 弹簧configuration.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"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        ">

    <!-- Placeholder config -->
    <context:property-placeholder location="classpath:database.properties" />

    <jpa:repositories base-package="org.mypackage.dao"/>

    <!-- Data source -->
    <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="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="org.mypackage.model" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
</beans>

The database.properties : database.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database
jdbc.username=root1
jdbc.password=password

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true

In the org.mypackage.model package, I have a class annotated with @Entity . org.mypackage.model包中,我有一个用@Entity注释的类。 The database exists, the user exists as well. 数据库存在,用户也存在。 When I run my application, the database is not updated with a new table that corresponds to my class. 当我运行应用程序时,数据库不会使用与我的班级相对应的新表进行更新。 How can I fix this? 我怎样才能解决这个问题?

EDIT 1: 编辑1:

Output when running the application (server starting omitted, no errors): 运行应用程序时的输出(服务器启动省略,没有错误):

INFO: Server startup in 157 ms
Connected to server
[2015-10-11 04:17:57,948] Artifact project:war exploded: Artifact is being deployed, please wait...
2015-10-11 16:18:00,274  INFO DispatcherServlet:484 - FrameworkServlet 'mvc-dispatcher': initialization started
2015-10-11 16:18:00,298  INFO XmlWebApplicationContext:510 - Refreshing WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Sun Oct 11 16:18:00 CEST 2015]; root of context hierarchy
2015-10-11 16:18:00,342  INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
2015-10-11 16:18:01,066  INFO RequestMappingHandlerMapping:220 - Mapped "{[/person/add],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.mypackage.controller.PersonController.home()
2015-10-11 16:18:01,067  INFO RequestMappingHandlerMapping:220 - Mapped "{[/person/save],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.mypackage.controller.PersonController.save(org.mypackage.model.Person,org.springframework.validation.BindingResult)
2015-10-11 16:18:01,069  INFO RequestMappingHandlerMapping:220 - Mapped "{[/hello],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.mypackage.controller.HelloController.hello()
2015-10-11 16:18:01,119  INFO Version:27 - HV000001: Hibernate Validator 5.0.3.Final
2015-10-11 16:18:01,342  INFO RequestMappingHandlerAdapter:523 - Looking for @ControllerAdvice: WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Sun Oct 11 16:18:00 CEST 2015]; root of context hierarchy
2015-10-11 16:18:01,386  INFO RequestMappingHandlerAdapter:523 - Looking for @ControllerAdvice: WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Sun Oct 11 16:18:00 CEST 2015]; root of context hierarchy
2015-10-11 16:18:01,492  INFO DispatcherServlet:503 - FrameworkServlet 'mvc-dispatcher': initialization completed in 1215 ms
[2015-10-11 04:18:01,516] Artifact project:war exploded: Artifact is deployed successfully
[2015-10-11 04:18:01,516] Artifact project:war exploded: Deploy took 3 568 milliseconds
2015-10-11 16:18:02,723  WARN PageNotFound:1120 - No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'mvc-dispatcher'

pom.xml: 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>com.springapp</groupId>
    <artifactId>project</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>project</name>

    <properties>
        <spring.version>4.1.1.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.9.Final</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.3.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.0.3.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>


    </dependencies>

    <build>
        <finalName>project</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Tests.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

You can change your properties this 您可以更改属性

hibernate.hbm2ddl.auto=update

to this 对此

hibernate.hbm2ddl.auto=create

Your DB will be created from zero. 您的数据库将从零开始创建。 You may want to take the back up of your db before doing this. 在执行此操作之前,您可能需要备份数据库。

Have you tried with the "annotatedClasses" property? 您是否尝试过使用“ annotatedClasses”属性? I remember having the same issue before since it didn't seem to get the annotated classes so I solved it with that property, the usage below: 我记得之前也遇到过同样的问题,因为它似乎没有获得带注释的类,因此我使用该属性(以下用法)解决了该问题:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <value>
                hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
                hibernate.format_sql = true
                hibernate.show_sql = true
                hibernate.hbm2ddl.auto = create
            </value>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>org.company.project.model.User</value>
                <value>org.company.project.model.Document</value>
            </list>
        </property>
        <!-- <property name="packagesToScan"> -->
        <!-- <list> -->
        <!-- <value>org.company.project.model</value> -->
        <!-- </list> -->
        <!-- </property> -->
    </bean>

I´ve created a simple application using Spring 4.1.1.RELEASE . 我已经使用Spring 4.1.1.RELEASE创建了一个简单的应用程序。

The application is running from the main method of the App.class . 该应用程序是从App.class的main方法运行的。

The configuration used was similar to yours, the only difference being the database choosen (postgresql instead of mysql). 使用的配置与您的配置相似,唯一的区别是选择了数据库(PostgreSQL代替mysql)。

In the example, an additional bean was created called jpaVendorAdapter . 在示例中,创建了另一个名为jpaVendorAdapter bean。 The configuration is as follows: 配置如下:

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true"/>
    <property name="generateDdl" value="true"/>
    <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
</bean>

com.stackoverflow.App com.stackoverflow.App

public class App {

    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("spring/Datasource.xml");
    }

}

resources/properties/database.properties 资源/属性/ database.properties

jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/spring
jdbc.username=postgres
jdbc.password=abc123

resources/spring/Datasource.xml 资源/弹簧/ Datasource.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.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>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.stackoverflow.model"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true"/>
        <property name="generateDdl" value="true"/>
        <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
    </bean>

</beans>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM