2

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

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

6
  • I assume you have edited the serverurl and database name in the post? Commented Mar 20, 2013 at 9:28
  • it will be funny if i doesn't =P, yes, they've been edited for security purpose, maybe i'm a bit paranoic.. Commented Mar 20, 2013 at 9:37
  • Thats the right thing to do, I was just making sure it was intentional Commented Mar 20, 2013 at 9:48
  • yes it was, and i'm quite sure that the connection string is correct, but now i think that maybe i don't have to put also the database name in the hibernate.connection.url property, if there is a specific property (i'm guessing) for the db name.. going to search Commented Mar 20, 2013 at 10:11
  • Found the problem: I wrote: hb_props.put("hibernate.connection.driver.class", "com.microsoft.sqlserver.jdbc.SQLServerDriver"); instead of: hb_props.put("hibernate.connection.driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver"); Commented Mar 20, 2013 at 10:19

1 Answer 1

2

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 guide from JBoss uses an _ instead of a .

Sign up to request clarification or add additional context in comments.

1 Comment

That's the answer ;), and I have to say also that the way i declare the property "hibernate.connection.url" it's wrong, instead of "jdbc:sqlserver://serverurl//dbname" the correct string is like: "jdbc:sqlserver://serverurl;databaseName=dbname". In many post I saw that in the hibernate config xml it's written in the first way, i wonder if it's different when it's passed runtime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.