我的应用程序在内存数据库中的 hypersql 上工作正常,但是,当我尝试迁移到 MSSQL 时,应用程序抛出异常。
我在POM文件中添加了mssql的依赖项:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version>
<scope>test</scope>
</dependency>
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
关于java - 将 Hibernate xml 配置文件从 hypersql 转换为 mssql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57120584/