java SQLServer 的休眠配置 - 驱动程序类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15519621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:54:39  来源:igfitidea点击:

Hibernate Configuration for SQLServer - Driver class

javasql-serverhibernatejdbcdriver

提问by Stefano Vercellino

I'm trying to configure hibernate with a SQLServer db.

我正在尝试使用 SQLServer 数据库配置休眠。

The configuration:

配置:

public static SessionFactory getSessionFactory() {
    try {
        if (null==sessionFactory) {
            Properties hb_props = new Properties();
            hb_props.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2005Dialect");
            hb_props.put("hibernate.connection.driver.class", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
            hb_props.put("hibernate.connection.username", "someusername");
            hb_props.put("hibernate.connection.password", "somepassword");
            hb_props.put("hibernate.connection.url", "jdbc:sqlserver://serverurl//dbname");
            Configuration configuration = new Configuration();
            configuration.setProperties(hb_props);
            sessionFactory = configuration.addAnnotatedClass(Test.class).buildSessionFactory();
        }
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    return sessionFactory;
}

And I get the following error:

我收到以下错误:

[main] WARN org.hibernate.connection.DriverManagerConnectionProvider - no JDBC Driver class was specified by property hibernate.connection.driver_class
[main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: null at URL: jdbc:sqlserver://serverurl//dbname
[main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=someusername, password=****, driver.class=com.microsoft.sqlserver.jdbc.SQLServerDriver}
[main] WARN org.hibernate.cfg.SettingsFactory - Could not obtain connection to query metadata
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://serverurl//dbname
    at java.sql.DriverManager.getConnection(DriverManager.java:602)
    at java.sql.DriverManager.getConnection(DriverManager.java:154)
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:113)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
        .....

I'm using sqljdbc-1.2.jar and the driver class seems correctly spelled, cant't figure where is the flaw..

我正在使用 sqljdbc-1.2.jar 并且驱动程序类似乎拼写正确,无法弄清楚缺陷在哪里..

采纳答案by Kevin Bowersox

Try changing this:

尝试改变这个:

hb_props.put("hibernate.connection.driver.class",
    "com.microsoft.sqlserver.jdbc.SQLServerDriver");

To this:

对此:

hb_props.put("hibernate.connection.driver_class",
   "com.microsoft.sqlserver.jdbc.SQLServerDriver");

This configuration guidefrom JBoss uses an _instead of a .

JBoss 的这个配置指南使用一个_而不是一个.