【发布时间】:2016-02-12 18:23:15
【问题描述】:
我的项目有问题,我尝试在不使用 hibernate.cfg.xml 文件的情况下配置 hibernate,我是这样做的:
private SessionFactory buildSessionFactory() {
try {
System.out.println(hostname);
System.out.println(username);
System.out.println(password);
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration()
.addAnnotatedClass(DBUser.class)
.addAnnotatedClass(DBTrustee.class)
.addAnnotatedClass(DBTrusteeUser.class)
.setProperty("hibernate.show_sql", "true")
.setProperty("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver")
.setProperty("connection.url", "jdbc:oracle:thin:@"+hostname)
.setProperty("connection.username", username)
.setProperty("connection.password", password)
.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect")
.setProperty("hibernate.default_schema", "apps")
.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
它看不到我的构建路径中的 OJDBC6 驱动程序,当我使用 hibernate.cfg.xml 文件时可以看到,但是当我尝试避免使用文件时,我得到了这个:
警告:HHH000181:没有遇到合适的连接提供程序,假设应用程序将提供连接 2016 年 12 月 12 日下午 7:26:06 org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator 启动服务 警告:HHH000342:无法获得查询元数据的连接:应用程序必须提供 JDBC 连接 2016 年 12 月 12 日下午 7:26:06 org.hibernate.dialect.Dialect
我做错了什么?我尝试使用这两个 jdbc 驱动程序类: oracle.jdbc.driver.OracleDriver 和 oracle.jdbc.OracleDriver
仍然没有运气:(
【问题讨论】:
-
provider 和驱动不一样,需要检查驱动不是问题是provider
-
所以这是我之前使用 xml 文件的尝试:
<session-factory> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@ip</property> <property name="hibernate.connection.username"></property> <property name="hibernate.connection.password"></property> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="hibernate.default_schema">apps</property> <property name="show_sql">true</property>它工作得很好...... :( 没有提供者。
标签: java spring oracle hibernate jdbc