我的应用程序在内存数据库中使用hypersql工作正常,但是,当我尝试移动到MSSQL时,应用程序会抛出异常。
我在 POM 文件中添加了 mssql 的依赖项:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version>
<scope>test</scope>
</dependency>
以下 NOT-WORKING 代码用于 hibernate xml 文件中的 mssql:
<hibernate-configuration>
<session-factory>
<property name="hibernate.archive.autodetection">class,hbm</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.username">ABCD</property>
<property name="hibernate.connection.password">0000</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databasename=c1;</property>
<property name="hibernate.hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
以下 hibernate xml 文件中的 hypersql 工作代码:
<hibernate-configuration>
<session-factory>
<property name="hibernate.archive.autodetection">class,hbm</property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:hsqldb:mem:howtodoinjava</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.acquire_increment">1800</property>
<property name="hibernate.c3p0.max_statements">150</property>
</session-factory>
</hibernate-configuration>
应用程序引发异常,如下所示:
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
引起:
java.lang.ClassNotFoundException: Could not load requested class : com.microsoft.sqlserver.jdbc.SQLServerDriver
org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.microsoft.sqlserver.jdbc.SQLServerDriver]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory'
您的连接设置链接到不正确的驱动程序。
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
将其指向您在pom中导入的同一位置.xml(<groupId>com.microsoft.sqlserver</groupId>
(
com.microsoft.sqlserver.jdbc.SQLServerDriver