有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java hibernate。cfg。在spring mv3应用程序中找不到xml

我正在使用SpringMVC3和hibernate3。在我的应用程序中,我需要使用servlet。 在这个Servlet中,我必须调用我的DAO层。但当我在servlet中使用以下代码时

Session session = HibernateUtil.getSessionFactory().openSession();
public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

我得到了错误组织。冬眠冬眠例外:冬眠。cfg。找不到xml

当前,我的xml文件与其他配置文件一起位于WEB-INF文件夹中

所有的解决方案都说我需要将它保存在src文件夹中,以便在运行时自动获取它。但我在这里使用的是SpringMVC。 所以我有点困惑,请帮助我如何解决它

我使用控制器的所有其他地方工作正常

enter image description here

我在拼车servlet中使用以下条目。xml

下面是我的拼车冬眠。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: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.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            ">

    <context:property-placeholder location="/WEB-INF/dbproperties.properties" />

    <!-- Enable annotation style of managing transactions -->
    <tx:annotation-driven transaction-manager="transactionManager" />   

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                 p:dataSource-ref="dataSource"
                 p:configLocation="${hibernate.config}"
                 p:packagesToScan="store.custom.controllers">

                <property name="annotatedClasses">
        <list>
            <value>common.domain.Ride</value>
            <value>common.businessclass.PostAdRR</value>
        </list>
    </property>
                 </bean>

    <!-- Declare a datasource that has pooling capabilities-->   
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
                destroy-method="close"
                p:driverClass="${jdbc.driverClassName}"
                p:jdbcUrl="${jdbc.url}"
                p:user="${jdbc.username}"
                p:password="${jdbc.password}"
                p:acquireIncrement="5"
                p:idleConnectionTestPeriod="60"
                p:maxPoolSize="100"
                p:maxStatements="50"
                p:minPoolSize="10" />

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

</beans>

下面是我的冬眠。cfg。xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


<hibernate-configuration>

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

</hibernate-configuration>

现在如何设置我的hibernate配置。xml 请帮忙


共 (2) 个答案

  1. # 1 楼答案

    冬眠在哪里。正在设置配置

    p:configLocation="${hibernate.config}"
    

    如果您使用的是maven,那么可以将该文件移动到src/main/resources,然后执行classpath:hibernate。配置。xml,否则,如果不是,请将其移动到WEB-INF/classes下的classpath中,如duffymo所述

  2. # 2 楼答案

    我建议搬家。cfg。将xml导入类路径。这意味着把它放在WEB-INF/类中,而不是WEB-INF