spring mvc 3 with hibernate(和hibernate.hbm2ddl.auto)

时间:2013-09-13 14:13:33

标签: java spring hibernate spring-mvc

设置spring mvc 3并使用auto dll进行休眠时遇到问题 这是我的文件夹结构: Folder structure

这些是我的图书馆:

Libraries

AbstractUser

package org.andreadorigo.abstracts;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * Generic user
 * @author andreadorigo
 *
 */

public abstract class AbstractUser {
    @Id
    @Column(name="id")
    @GeneratedValue
    private Integer id;

    @NotEmpty
    @Email
    @Column(name="email")
    public String email;

    @NotEmpty
    @Size(min = 6, max = 14)
    @Column(name="password")
    public String password;

    /**
     * 
     */
    public AbstractUser() {
        super();
    }


    //Getters and setters
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



}

CustomerUser

package org.andreadorigo.entities;

import javax.persistence.Entity;
import javax.persistence.Table;

import org.andreadorigo.abstracts.AbstractUser;

@Entity
@Table(name="customerUser")
public class CustomerUser extends AbstractUser{

}

调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   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.0.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


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

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

    <bean
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactory">
        <property
            name="persistenceUnitName"
            value="jtdb" />

        <property
            name="dataSource"
            ref="dataSource" />

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

        <property
            name="showSql"
            value="true" />

        <property
            name="generateDdl"
            value="true" />

        <property
            name="databasePlatform"
            value="org.hibernate.dialect.MySQLDialect" />
        </bean>
    </property>

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>
</beans>

hibernate.cfg.xml中

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="transaction.factory_class">org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
    </session-factory>
</hibernate-configuration>

的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="CustomerUser">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>org.andreadorigo.entities.CustomerUser</class>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>
    </persistence-unit>

</persistence>

但是当我运行jboss 7时,我收到了这些警告/错误:

15:48:57,442 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.AbstractEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/AbstractEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)
    15:48:58,322 INFO  [org.hibernate.validator.util.Version] (MSC service thread 1-2) Hibernate Validator 4.2.0.Final
15:48:59,172 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)

15:48:59,614 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-2) Monitoring jndi:/default-host/GenericProject/WEB-INF/faces-config.xml for modifications
15:49:00,295 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.AbstractEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/AbstractEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)
    15:49:00,405 INFO  [org.jboss.web] (MSC service thread 1-2) JBAS018210: Registering web context: /GenericProject
15:49:01,010 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)

15:49:01,271 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.AbstractEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/AbstractEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)

15:49:01,529 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)

15:49:01,778 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-3) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
15:49:01,795 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.AbstractEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/AbstractEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)

15:49:02,061 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)
15:49:02,310 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-3) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@55e3c942: defining beans [registerController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,viewResolver,entityManagerFactory]; root of factory hierarchy
15:49:02,374 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.AbstractEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/AbstractEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)
    15:49:02,749 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)
15:49:03,126 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.AbstractEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/AbstractEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)
    15:49:03,505 WARN  [org.jboss.modules] (MSC service thread 1-3) Failed to define class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in Module "deployment.SpringTest.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean (Module "deployment.SpringTest.war:main" from Service Module Loader)

15:49:03,839 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-3) Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@55e3c942: defining beans [registerController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,viewResolver,entityManagerFactory]; root of factory hierarchy
15:49:03,875 ERROR [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-3) Destroy method on bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#0' threw an exception: java.lang.NoClassDefFoundError: org/springframework/transaction/support/TransactionSynchronization

15:49:04,031 ERROR [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-3) Destroy method on bean with name '(inner bean)' threw an exception: java.lang.NoClassDefFoundError: org/springframework/transaction/support/TransactionSynchronization


15:49:04,182 ERROR [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-3) Destroy method on bean with name 'org.springframework.format.support.FormattingConversionServiceFactoryBean#0' threw an exception: java.lang.NoClassDefFoundError: org/springframework/transaction/support/TransactionSynchronization
    15:49:04,381 ERROR [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-3) Destroy method on bean with name 'registerController' threw an exception: java.lang.NoClassDefFoundError: org/springframework/transaction/support/TransactionSynchronization

15:49:04,530 ERROR [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-3) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] for bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean from [Module "deployment.SpringTest.war:main" from Service Module Loader]
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] for bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean from [Module "deployment.SpringTest.war:main" from Service Module Loader]

15:49:04,868 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/SpringTest]] (MSC service thread 1-3) StandardWrapper.Throwable: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] for bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean from [Module "deployment.SpringTest.war:main" from Service Module Loader]
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] for bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean from [Module "deployment.SpringTest.war:main" from Service Module Loader]


15:49:05,194 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/SpringTest]] (MSC service thread 1-3) Servlet /SpringTest threw load() exception: java.lang.ClassNotFoundException: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean from [Module "deployment.SpringTest.war:main" from Service Module Loader]

2 个答案:

答案 0 :(得分:1)

这里至少有两件事是错误的

  1. 你正在混合弹簧版本3.0.3和3.1.1,永远不要混合不同的弹簧版本(实际上适用于所有框架)。
  2. 你至少错过了spring-tx.jar和spring-aop.jar。
  3. 我强烈建议您使用MavenGradle来管理您的依赖关系,这会让您在搜索互联网上的垃圾时遇到很多麻烦。

    接下来,您要考虑要用于配置的内容,您可以通过hibernate.cfg.xml,spring上下文文件和persistence.xml配置您的entitymanager。为什么?至少删除hibernate.cfg.xml并将所需的配置放在persistence.xml中。

答案 1 :(得分:0)

在此行

下,您似乎错过了类路径中的spring-tx jar
15:49:04,031 ERROR [org.springframework.beans.factory.support.DefaultListableBeanFactory] 
(MSC service thread 1-3) Destroy method on bean with name '(inner bean)' 
threw an exception: java.lang.NoClassDefFoundError: org/springframework/transaction/support/TransactionSynchronization

你可以得到它here

你实际上似乎缺少了更多。

java.lang.ClassNotFoundException: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean 

here获取所有弹簧罐。