【发布时间】:2012-05-04 04:25:51
【问题描述】:
我下载了 Hibernate 4.1.2 并使用 Oracle Database 10g 第 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"); 加载驱动程序,它工作正常。
【问题讨论】:
-
属性不应该用
=字符分隔吗? IE。hibernate.dialect=org.hibernate.dialect.OracleDialect等等? -
@mcfinnigan,好点子,但在例外情况下,我看到“没有适合 jdbc:oracle:thin:@localhost:1521/xe 的驱动程序”,所以它看起来可以正常读取文件。跨度>
-
嗯。你确定 oracle jar 在类路径上吗?
-
是的。我可以通过
Class.forName加载它。 -
如果在构造
Configuration之前添加Class.forName(...)会怎样?