2014-05-11 27 views
0

我已經遵循了一些教程,以便使用spring建立一個web應用程序,在tomcat上運行hibernate。當'hibernate.dialect'未設置時,連接不能爲空hibernate4 tomcat7

當試圖運行我的應用程序出現錯誤「連接不能爲空當‘hibernate.dialect’未設置」

我的hibernate.cfg.xml是

<?xml version='1.0' encoding='utf-8'?> 
    <!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate ConfigurationDTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

    <hibernate-configuration> 
     <session-factory> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/myproject</property> 
     <property name="connection.username">root</property> 
    <property name="connection.password">root</property> 
     <mapping class="com.myproject.entity.user.UserEntity"></mapping> 
     <mapping class="com.myproject.entity.user.UserAboutEntity"></mapping> 
     <mapping class="com.myproject.entity.user.UserBasicInfoEntity"></mapping> 
    <mapping class="com.myproject.entity.user.UserDescriptionEntity"></mapping> 
    <mapping class="com.myproject.entity.user.UserInterestsEntity"></mapping> 
    <mapping class="com.myproject.entity.user.UserEntity"></mapping> 
    <mapping class="com.myproject.entity.user.UserPersonalViewsEntity"></mapping> 
    <mapping class="com.myproject.entity.user.UserPetsEntity"></mapping> 
    <mapping class="com.myproject.entity.user.UserSportsEntity"></mapping> 

    <mapping class="com.myproject.entity.user.PartnerAboutEntity"></mapping> 
    <mapping class="com.myproject.entity.user.PartnerBasicInfoEntity"></mapping> 
    <mapping class="com.myproject.entity.user.PartnerDescriptionEntity"></mapping> 
    <mapping class="com.myproject.entity.user.PartnerInterestsEntity"></mapping> 
    <mapping class="com.myproject.entity.user.PartnerPersonalViewsEntity"></mapping> 
    <mapping class="com.myproject.entity.user.PartnerPetsEntity"></mapping> 
    <mapping class="com.myproject.entity.user.PartnerSportsEntity"></mapping> 
    <mapping class="com.myproject.entity.user.PartnerStyleEntity"></mapping> 


    <mapping class="com.myproject.entity.user.MessagePageEntity"></mapping> 
    <mapping class="com.myproject.entity.user.MessageEntity"></mapping> 

    <mapping class="com.myproject.entity.user.MatchListEntity"></mapping> 
    </session-factory> 
    </hibernate-configuration> 

我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.myproject.controller</groupId> 
    <artifactId>myproject</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <name>myproject Webapp</name> 
    <url>http://maven.apache.org</url> 
    <properties> 
    <spring.version>3.1.1.RELEASE</spring.version> 
    </properties> 
    <build> 
      <finalName>myproject-webapp</finalName> 
      <plugins> 
       <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>${jdk.version}</source> 
        <target>${jdk.version}</target> 
       </configuration> 
       </plugin> 
     <plugin> 
       <groupId>net.leadware</groupId> 
     <artifactId>hibernate4-maven-plugin</artifactId> 
     <version>1.0.1</version> 
     </plugin> 
      </plugins> 
     </build> 
     <repositories> 
       <repository> 
        <id>repository.jboss.org-public</id> 
        <name>JBoss repository</name> 
        <url>https://repository.jboss.org/nexus/content/groups/public</url> 
       </repository> 
      </repositories> 
      <dependencies> 
      <!-- Spring dependencies --> 
       <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-core</artifactId> 
        <version>${spring.version}</version> 
       </dependency> 
       <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> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc-portlet</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
       <dependency> 
      <groupId>org.springframework</groupId> 
       <artifactId>spring-tx</artifactId> 
       <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-expression</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aop</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context-support</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-oxm</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <!-- MySQL database driver --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.30</version> 
    </dependency> 

    <!-- Hibernate framework --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>4.2.7.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>4.2.7.Final</version> 
    </dependency>   

    <!-- Hibernate library dependecy start --> 
    <dependency> 
     <groupId>dom4j</groupId> 
     <artifactId>dom4j</artifactId> 
     <version>1.6.1</version> 
    </dependency> 

    <dependency> 
     <groupId>commons-logging</groupId> 
     <artifactId>commons-logging</artifactId> 
     <version>1.1.1</version> 
    </dependency> 

    <dependency> 
     <groupId>commons-collections</groupId> 
     <artifactId>commons-collections</artifactId> 
     <version>3.2.1</version> 
    </dependency> 

    <dependency> 
     <groupId>antlr</groupId> 
     <artifactId>antlr</artifactId> 
     <version>2.7.7</version> 
    </dependency> 
    <!-- Hibernate library dependecy end --> 

    <!-- Tomcat --> 
    <dependency> 
     <groupId>org.apache.tomcat</groupId> 
     <artifactId>tomcat-dbcp</artifactId> 
     <version>7.0.52</version> 
    </dependency> 

    <!-- Log4J --> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.15</version> 
     <exclusions> 
      <exclusion> 
       <groupId>javax.mail</groupId> 
       <artifactId>mail</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>javax.jms</groupId> 
       <artifactId>jms</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>com.sun.jdmk</groupId> 
       <artifactId>jmxtools</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>com.sun.jmx</groupId> 
       <artifactId>jmxri</artifactId> 
      </exclusion> 
     </exclusions> 
     <scope>runtime</scope> 
    </dependency> 

    <dependency> 
     <groupId>javassist</groupId> 
     <artifactId>javassist</artifactId> 
     <version>3.12.1.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
     <scope>runtime</scope> 
    </dependency> 
    <dependency> 
     <groupId>taglibs</groupId> 
     <artifactId>standard</artifactId> 
     <version>1.1.2</version> 
     <scope>runtime</scope> 
    </dependency> 
    <dependency> 
     <groupId>commons-dbcp</groupId> 
     <artifactId>commons-dbcp</artifactId> 
     <version>1.4</version> 
    </dependency> 
     </dependencies> 
    </project> 

用戶servlet.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:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:lang="http://www.springframework.org/schema/lang" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 

<context:annotation-config /> 
<context:component-scan base-package="com.findlove.controller" /> 

<bean id="jspViewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" 
     value="org.springframework.web.servlet.view.JstlView"></property> 
    <property name="prefix" value="/WEB-INF/view/"></property> 
    <property name="suffix" value=".jsp"></property> 
</bean> 

<bean id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="classpath:messages"></property> 
    <property name="defaultEncoding" value="UTF-8"></property> 
</bean> 
<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
    p:location="/WEB-INF/jdbc.properties"></bean> 

<bean id="dataSource" 
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
    p:driverClassName="${jdbc.driverClassName}" 
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" 
    p:password="${jdbc.password}"></bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="packagesToScan" value="com.findlove.entity" /> 
</bean> 

<bean id="userDAO" class="com.findlove.dao.user.UserDAOImpl"></bean> 
<bean id="userManager" class="com.findlove.service.user.UserManagerImpl"></bean> 

<tx:annotation-driven /> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"></property> 
</bean> 

任何意見,爲什麼我得到這個錯誤將不勝感激。

麗莎

+0

請發佈您如何從hibernate.cfg.xml加載配置以及您正在使用的Hibernate版本。 –

+1

你需要配置hibernate使用哪種方言,例如mysql' org.hibernate.dialect.MySQLDialect ' –

+0

你的數據庫是否真正啓動並運行? – geoand

回答

1

一段代碼你提到:

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="configLocation"> 
     <value>classpath:hibernate.cfg.xml</value> 
    </property> 
    <property name="configurationClass"> 
     <value>org.hibernate.cfg.AnnotationConfiguration</value> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 

這是指定hibernate.cfg.xml的地方,也是放置方言的地方。如果你有會話工廠的<property name="dataSource" ref="dataSource" />,你不需要它在hibernate.cfg.xml中。 我會建議再次查看教程源代碼並相應地調整您的代碼。

+0

我跟着你的建議,並解決了問題,謝謝。我的問題是教程是在hibernate3,我是使用hibernate4所以當更新hibernate4的配置時,我最終忽略了sessionFactory中的一些配置。非常感謝! –

0

從教程添加這一點,並檢查

<屬性名= 「方言」 > org.hibernate.dialect.MySQLDialect < /屬性>

+0

我添加和重建與maven但我仍然得到相同的錯誤:( –

相關問題