当前位置: 首页 > 面试题库 >

当'hibernate.dialect'未设置hibernate4 tomcat7时,连接不能为空

微生高谊
2023-03-14
问题内容

我遵循了一些教程,以便使用spring,hibernate模式在tomcat上运行来设置Web应用程序。

尝试运行我的应用程序时,出现错误“未设置’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>

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

关于为什么我会收到此错误的任何建议将不胜感激。

丽莎


问题答案:

您提到的教程中的一段代码

    <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中不需要它。我建议再次查看该教程的源代码并相应地调整您的代码。



 类似资料: