0

我是 hibernate 和 Spring 的新手,并希望获得一个简单的示例。这是我的spring 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: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"
    default-autowire="byType">



    <bean id="duke" class="com.springinaction.springidol.Juggler">
        <constructor-arg value="15" />
    </bean>

    <bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
        <property name="song" value="Jingle Bells" />
    </bean>

    <bean id="sonet29" class="com.springinaction.springidol.Sonnet29" />
    <bean id="stage" class="com.springinaction.springidol.Stage"
        factory-method="getInstance" />
    <bean id="saxophone" class="com.springinaction.springidol.Saxophone" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://ec2-21-21-214-251.compute-1.amazonaws.com:5432/d7zxdletq2mmv'" />
        <property name="username" value="eqwosdacrklhxpq" />
        <property name="password" value="wkAasO8d-hdr2hGoQXYadbjMKhGR" />
        <property name="initialSize" value="1" />
        <property name="maxActive" value="1" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.springinaction.springidol.models" />
        <property name="hibernateProperties">
            <props>
                <prop key="dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            </props>
        </property>
    </bean>

</beans>

我的 pom.xml 依赖项如下

<properties>
        <spring.version>3.1.1.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!-- Spring framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency> <!-- Used for Hibernate4 LocalSessionFactoryBean -->
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- AOP dependency -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>

        <!-- Persistence Management -->
        <dependency> <!-- Apache BasicDataSource -->
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>

        <dependency> <!-- Hibernate -->
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.3.Final</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.2-1002-jdbc4</version>
        </dependency>

    </dependencies>

我的应用程序是一个简单的命令行应用程序。

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/springinaction/springidol/testing.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

我究竟做错了什么。我想补充一点,我的模型目录是空的。

亲切的问候

4

1 回答 1

2

在您定义 Hibernate 属性的部分中,它应该如下所示。所有键都应以 为前缀hibernate.

<property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
    </props>
</property>
于 2013-10-24T07:49:20.147 回答