0

spring mvc3 と hibernate3 を使用しています。私のアプリケーションでは、サーブレットを使用する必要があります。そのサーブレットで、DAO レイヤーを呼び出す必要があります。しかし、サーブレットで次のコードを使用している場合。

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 not found が表示されます。

現在、私のxmlファイルは、他の構成ファイルとともにWEB-INFフォルダーにあります。

すべてのソリューションは、実行時に自動的に取得されるように、src フォルダーに保持する必要があると言っています。しかし、ここではSpring MVCを使用しています。だから私は少し混乱していますそれを解決する方法を教えてください。

コントローラーを使用している他のすべての場所は正常に動作しています。

ここに画像の説明を入力

carpool-servlet.xml で次のエントリを使用しています

以下は私のcarpool-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>

hibernate config.xmlを設定するにはどうすればよいですか 助けてください

4

2 に答える 2

1

hibernate.config が設定されている場所

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

Maven を使用している場合は、このファイルを src/main/resources に移動してから classpath:hibernate.config.xml を実行できます。そうでない場合は、duffymo が述べたように WEB-INF/classes の下のクラスパス内に移動します。

于 2012-05-11T16:26:20.953 に答える
1

.cfg.xml を CLASSPATH に移動することをお勧めします。つまり、WEB-INF ではなく、WEB-INF/classes に配置します。

于 2012-05-11T15:06:27.813 に答える