用HIbernate+Oracle整合的时候遇到了一个问题,运行Main方法的时候不报错,也没有任何的提示信息,但是却无法创建表。
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
System.out.println(session.isConnected());
Transaction tx = session.beginTransaction();
tx.begin();
User user = new User();
user.setAge(10);
user.setBirthday(new Date());
user.setSex("man");
user.setUsername("Jone");
tx.commit();
session.close();
System.out.println("success");
}
相关配置文件如下:
hibernate.cfg.xml
<!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 name="hibernate_oracle">
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">simplelove</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521/app</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/myHibernate/domain/hbm/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.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 package="com.myHibernate.domain">
<class name="User" table="User">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<property name="sex"></property>
<property name="age"></property>
<property name="birthday"></property>
</class>
</hibernate-mapping>
仔细重复的查看配置文件,没有错误。检查账户权限,拥有所有权限。而且能够链接上数据库,就是无法创建表。后来Google一大圈之后,看到有一篇帖子说“Oracle数据库中,一个数据库下可以有多个表空间,多个用户可以有不同的表空间,如果用户A的表空间中已经存在table1,则使用如上方式在用户B的表空间再自动创建table1是不行的。”然后回头一看,我定义的表名就叫 User,这个名字太普通了,Oracle数据库中已经存在这张表了。把表名改了一下就可以了。