我的应用程序在内存数据库中与hypersql一起正常工作,但是,当我尝试移至MSSQL时,应用程序将引发异常。
我在POM文件中添加了mssql的依赖项:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version>
<scope>test</scope>
</dependency>
休眠xml文件中mssql的以下NOT-WORKING代码:
<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'
答案 0 :(得分:0)
您的连接设置已链接到错误的驱动程序。
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
将其指向您在pom.xml(<groupId>com.microsoft.sqlserver</groupId>
)中导入的同一文件
com.microsoft.sqlserver.jdbc.SQLServerDriver