我下载了Hibernate 4.1.2,使用的是Oracle Database 10g Release 2。我正在使用的 JDBC 驱动程序是 ojdbc14.jar
.
我将HibernateUtil类设置为:
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;
}
}
hibernate.properties
我有:
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
但是Hibernate不想加载驱动程序。它会抛出一个异常,指出"未找到合适的驱动程序"。
我尝试用Class.forName("oracle.jdbc.driver.OracleDriver");
加载驱动程序,它工作正常。
问题在于使用了错误的JDBC Oracle驱动程序。当我尝试使用ojdbc6.jar
时,一切正常。
有几件事:
- 尝试通过在键和值之间放置 = 来使属性文件有效
- 检查值后是否没有任何尾随空格
- 使用
oracle.jdbc.OracleDriver
而不是oracle.jdbc.driver.OracleDriver
。请参阅Oracle jdbc 驱动程序类之间的差异?供进一步参考。
您的连接 URL 配置错误,应为:
hibernate.connection.url jdbc:oracle:thin:@localhost:1521:xe
有关甲骨文 URL 的更多信息,请参阅此处。
正如其他答案所指出的:
使用oracle.jdbc.OracleDriver
而不是oracle.jdbc.driver.OracleDriver