2013-10-24 85 views
0

我是hibernate和Spring的新手,希望得到一個簡單的示例工作。這裏是我的Spring XML配置PostgressSQL的春季休眠異常Dilect

<?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 

我做錯了什麼。我想補充說我的模型目錄是空的。

親切的問候

回答

2

在你定義Hibernate屬性的部分,它應該像下面這樣。所有密鑰的前綴應爲hibernate.

<property name="hibernateProperties"> 
    <props> 
     <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
    </props> 
</property> 
+0

好的。謝謝,這工作。現在我遇到了Session的問題。在Hibernate 4中,它們沒有導入org.hibernate.classic.Session;我該做什麼呢? – AndroidDev

+0

我認爲它可能在一段時間之前已經遷移到'org.hibernate.Session'。而且,我很確定您的IDE應該能夠幫助您找到合適的課程。 – nobeh