我正在使用spring mvc3和hibernate3。在我的应用程序中,我需要使用servlet。
在那个servelet中,我必须调用我的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;
    }
}


我收到错误org.hibernate.HibernateException:找不到hibernate.cfg.xml。

目前,我的xml文件和其他配置文件在WEB-INF文件夹中。

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

我正在使用控制器的所有其他地方都可以正常工作。



我在carpool-servlet.xml中使用以下条目



以下是我的拼车-hibernate.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>


以下是我的hibernate.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>


现在如何设置我的休眠config.xml
请帮忙

最佳答案

hibernate.config在哪里设置

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


如果使用的是maven,则可以将此文件移至src / main / resources,然后执行classpath:hibernate.config.xml;否则,请按照duffymo所述,将其移至WEB-INF / classes下的classpath中。

07-24 19:00
查看更多