2013-03-20 47 views
2

我想配置一个SQLServer数据库休眠。SQLServer的Hibernate配置 - 驱动程序类

配置:

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; 
} 

而且我得到以下错误:

[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) 
     ..... 

我使用sqljdbc-1.2.jar和驱动程序类似乎拼写正确,cant't到哪是漏洞..

+0

我假设你已经编辑在后SERVERURL和数据库的名字吗? – 2013-03-20 09:28:24

+0

这将是有趣的,如果我不= P, 是的,他们已被编辑的安全目的,也许我有点偏执.. – 2013-03-20 09:37:36

+0

这是正确的事情,我只是确保它是故意的 – 2013-03-20 09:48:14

回答

2

尝试修改此:

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

要这样:

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

configuration guide从JBoss使用的_而不是.

+0

这就是答案;),我不得不说,我声明属性“hibernate.connection.url”的方式是错误的,而不是“jdbc:sqlserver:// serverurl // dbname”,正确的字符串是这样的: “JDBC:SQLSERVER:// SERVERURL;的databaseName = DBNAME”。在很多文章中,我看到在hibernate config xml中它是以第一种方式编写的,我想知道它在运行时运行时是否不同 – 2013-03-20 11:07:44

相关问题