在 Jboss7 中配置休眠 3

Configure hibernate 3 in Jboss7

我正在尝试设置 Spring MVC + Hibernate WAR 以部署到 Jboss 7.1.1 Final。我的应用程序使用 Hibernate 3.6.1 到目前为止,我的理解是 Hibernate 4 与 AS 打包在一起,并且是默认的持久性提供程序。我没有使用 persistent.xml 文件配置。

我已按照 JBoss 中设置 Hibernate 3 所需的所有步骤进行操作。

  1. 转到 AS 安装并切换到 modules/org 文件夹。
  2. 为插槽 3 创建了存放 Hibernate 3 的文件夹
  3. 将 Hibernate3 jar 复制到这个新的 AS/modules/org/hibernate/3 文件夹中 (hibernate3-core.jar, hibernate3-commons-annotations.jar, hibernate3-entitymanager.jar, dom4j.jar, slf4j.jar, slf4j-api.jar, commons-collections.jar、antlr.jar、slf4j-api.jar、commons-collections.jar、antlr.jar 以及 Hibernate 3)
  4. 所需的任何其他 jar
  5. 创建了包含内容的 AS/modules/org/hibernate/3/module.xml 文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.0" name="org.hibernate" slot="3">
        <resources>
            <resource-root path="hibernate3-core.jar"/>
            <resource-root path="hibernate3-commons-annotations.jar"/>
            <resource-root path="hibernate3-entitymanager.jar"/>
            <resource-root path="javassist-3.12.0.GA.jar"/>
            <resource-root path="antlr-2.7.6.jar"/>  
            <resource-root path="commons-collections-3.1.jar"/>  
            <resource-root path="dom4j-1.6.1.jar"/>  
            <!-- Insert other Hibernate 3 jars to be used here -->
        </resources>
    
        <dependencies>
            <module name="org.jboss.as.jpa.hibernate" slot="3"/>
            <module name="asm.asm"/>
            <module name="javax.api"/>
            <module name="javax.persistence.api"/>
            <module name="javax.transaction.api"/>
            <module name="javax.validation.api"/>
            <module name="org.apache.ant"/>
            <module name="org.infinispan"/>
            <module name="org.javassist"/>
            <module name="org.slf4j"/>
        </dependencies>
    </module>
    

我的pom.xml(部分):

    <!-- Hibernate dependencies -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.6.Final</version>
        <exclusions>
            <exclusion>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
            </exclusion>
        </exclusions>
        <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.5.6-Final</version>
</dependency>
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>
</dependency>
 <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>3.0.0.ga</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate.common</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>4.0.5.Final</version>
    </dependency>

我的hibernate-context.xml(部分)

  <jpa:repositories base-package="com.gea.dvr.repository" />
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                 p:dataSource-ref="dataSource"
                 p:configLocation="${hibernate.config}"
                 p:packagesToScan="com.iana.dver"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
                p:driverClass="${app.jdbc.driverClassName}"
                p:jdbcUrl="${app.jdbc.url}"
                p:user="${app.jdbc.username}"
                p:password="${app.jdbc.password}"
                p:idleConnectionTestPeriod="30"
                p:maxPoolSize="100"
                p:maxStatements="50"
                p:minPoolSize="10" 
                p:maxIdleTime="60" />

    <!-- Declare a transaction manager-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
                p:sessionFactory-ref="sessionFactory" />                    

  <!-- Specify our ORM vendor -->
  <bean id="hibernateVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" 
              p:showSql="false"/>

我的hibernate.cfg.xml

<hibernate-configuration>
  <session-factory name="sessionFactory">
    <!-- We're using MySQL database so the dialect needs to MySQL as well-->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- Enable this to see the SQL statements in the logs-->
    <property name="show_sql">false</property>
    <!-- This will drop our existing database and re-create a new one.
            Existing data will be deleted! -->
    <!--  <property name="hbm2ddl.auto">update</property>-->

    <mapping class="com.gea.dvr.domain.DvrUserType"/>
    <mapping class="com.gea.dvr.domain.DvrUsers"/>
    <mapping class="com.gea.dvr.domain.DvrConfig"/>
    <mapping class="com.gea.dvr.domain.DvrDetail"/>
    <mapping class="com.gea.dvr.domain.DvrFiles"/>
    <mapping class="com.gea.dvr.domain.DvrNotif"/>
    <mapping class="com.gea.dvr.domain.UserLogin"/>
  </session-factory>
</hibernate-configuration>

我已经提供了所有必需的 java 但我仍然遇到以下异常, 谁能帮我在 JBoss 中设置与 hibernate 3 相关的更改?

11:03:14,597 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-3) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateVendor' defined in ServletContext resource [/WEB-INF/hibernate-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence

由于您得到 java.lang.NoClassDefFoundError(而不是 ClassNotFoundException),这可能是某种类加载问题。

尝试添加 hibernate-entitymanager 依赖项来解决您的问题。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>${hibernate.version}</version>
    <exclusions>
        <exclusion>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
        </exclusion>
        <exclusion>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>