2

I downloaded Hibernate 4.1.2 and am using Oracle Database 10g Release 2. The JDBC driver I am using is ojdbc14.jar.

I set up HibernateUtil class as:

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        // Create the SessionFactory from hibernate.cfg.xml
        try{
            Configuration configuration = new Configuration();
            configuration.configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
            return configuration.buildSessionFactory(serviceRegistry);
        }catch(HibernateException ex){
            ex.printStackTrace();
            throw ex;
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

In hibernate.properties I have:

hibernate.dialect org.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username HR
hibernate.connection.password HR
hibernate.connection.url jdbc:oracle:thin:@localhost:1521/xe

But Hibernate doesn't want to load the driver. It throws an exception saying 'No appropriate driver found'.

I tried to load the driver with Class.forName("oracle.jdbc.driver.OracleDriver"); and it works fine.

nwinkler
  • 52,665
  • 21
  • 154
  • 168
Martin Dimitrov
  • 4,796
  • 5
  • 46
  • 62
  • 2
    Should properties not be separated by an `=` character? I.e. `hibernate.dialect=org.hibernate.dialect.OracleDialect` etc? – mcfinnigan Apr 23 '12 at 14:37
  • @mcfinnigan, good point but in the exception I see 'no suitable driver for jdbc:oracle:thin:@localhost:1521/xe' so it looks like it is reading the file fine. – Martin Dimitrov Apr 23 '12 at 14:44
  • hmm. Are you sure that the oracle jar is on the classpath? – mcfinnigan Apr 23 '12 at 14:49
  • Yes. I can load it through `Class.forName`. – Martin Dimitrov Apr 23 '12 at 14:51
  • Is this a web application? If so, are you *positive* that the oracle jarfiles are loaded and on the classpath when Hibernate is started? That's the only other possibility I can think of. – mcfinnigan Apr 23 '12 at 14:54
  • 1
    What if you add `Class.forName(...)` before construction of `Configuration`? – axtavt Apr 23 '12 at 15:03
  • @axtavt if I do as you suggest everything is working fine but I want to make Hibernate load the driver. – Martin Dimitrov Apr 24 '12 at 07:33
  • @Martin: It looks like Hibernate doesn't see `hibernate.connection.driver_class` property. Make sure that it's spelled correctly and you don't override it in `hibernate.cfg.xml`. – axtavt Apr 24 '12 at 08:31

3 Answers3

4

The problem was in using the wrong JDBC Oracle driver. When I tried with ojdbc6.jar everything worked fine.

Martin Dimitrov
  • 4,796
  • 5
  • 46
  • 62
1

A couple of things:

  • Try to make the properties file valid by putting = between key and value
  • Check that there aren't any trailing spaces after the values
  • Use oracle.jdbc.OracleDriver instead of oracle.jdbc.driver.OracleDriver. See Difference between Oracle jdbc driver classes? for further reference.
Community
  • 1
  • 1
nwinkler
  • 52,665
  • 21
  • 154
  • 168
1

Your connection URL is configured wrongly, should be:

hibernate.connection.url jdbc:oracle:thin:@localhost:1521:xe

More information for Oracle's URL can refer here.

As other answer point out:

Use oracle.jdbc.OracleDriver instead of oracle.jdbc.driver.OracleDriver

Community
  • 1
  • 1
Pau Kiat Wee
  • 9,485
  • 42
  • 40
  • Doesn't seem to matter whether I use `...@localhost:1521:xe` or `...@localhost:1521/xe`. As I said through JDBC I am able to connect with exactly the same settings. – Martin Dimitrov Apr 24 '12 at 07:31