日期:2014-05-19  浏览次数:20663 次

hibernate4.1.2配置连接oracle10g
我的hibernate4.1.2和oracle连接出现问题,帮忙看下,我用的oracle驱动包是ojdbc14.jar
hibernate.cfg.xml配置:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
  <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:WJLMGQS</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="show_sql">true</property>
  <property name="hbm2ddl.auto">create</property>
  <property name="format_sql">true</property>
  <mapping resource="org/wjlmgqs/person/person.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

上面配的是oracle,如果换成mysql的就没问题了,

person.hbm.xml配置:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="org.wjlmgqs.person.Person">
<id name="id" >
<generator class="native"></generator>
</id>
<property name="name" />
<property name="age" />
  </class>
</hibernate-mapping>
Person对象属性:
int id;
String name ;
int age ;


测试代码:

public static void main(String[] args) {
Session session = new Configuration().configure().buildSessionFactory().openSession();
Person p = new Person();
session.beginTransaction();
p.setAge(3);
p.setName("wjl");
session.save(p);
session.getTransaction().commit();
session.close();
}


出现错误:
19:57:58,424 WARN org.hibernate.internal.util.xml.DTDEntityResolver:74 - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
19:58:02,184 WARN org.hibernate.engine.jdbc.internal.JdbcServicesImpl:169 - HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:oracle:thin:@localhost:1521:WJLMGQS
19:58:06,287 INFO org.hibernate.tool.hbm2ddl.SchemaExport:343 - HHH000227: Running hbm2ddl schema export
19:58:06,289 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:353 - Import file not found: /import.sql
19:58:06,359 ERROR org.hibernate.tool.hbm2ddl.SchemaExport:385 - HHH000231: Schema export unsuccessful
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:WJLMGQS
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.DatabaseExp