Gd上次和大家谈到使用DDL语句实现自动建表失败的问题,过了几天再回头看果然思路就变得清晰许多,原来是xxx.hbm.xml文件的配置上出了问题,因为上次没有用Junit进行测试,所以没有看明白问题的描述,这里不得不说一句,Junit真是个好东西,emmmmmm。Junit测试后报的错误是这样的: Could not parse mapping document: org/User/hibernate.hbm.xml (RESOURCE),大概意思就是无法解析这个xxx.hbm.xml文件,对于初学者来说,我觉得应对这么大范围的错误最好的办法就是把以前用的正确的xxx.hbm.xml文件拷贝过来测试成功了再说,Gd把之前测试成功的xxx.hbm.xml文件拷贝过来,再次进行测试,果不其然,测试成功!
![](https://img-blog.csdn.net/20180607101329556?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5MjA5MzYx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![](https://img-blog.csdn.net/20180607120324675?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5MjA5MzYx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
再回过头去找之前的错误所在,发现是粗心大意的自己把generator class那一行给落下了,真的是因小失大啊!!!
下面给出我的测试成功的文件供大家借鉴:
1.xxx.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">
<!-- Generated 2017-8-22 10:37:16 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="org.User.User" table="X_USER">
<id name="id" type="int">
<column name="ID" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<property name="gender" type="java.lang.String">
<column name="Gender" />
</property>
</class>
</hibernate-mapping>
这里的generator class属性值是increment,使用native也一样会测试成功。
2.xxx.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>
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<!-- 自动建表语句,其属性值有多个,create是其中一个,属性值不同功能不同 -->
<property name="hibernate.hbm2ddl.auto" >create</property>
<mapping resource="org/User/hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.测试类
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import org.User.User;
public class TestX_user{
@Test
public void testIncrease(){
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
try {
User u = new User();
u.setName("张三");
u.setAge(23);
u.setPassword("zhangsan");
session.save(u);
tx.commit();
} catch (Exception e)
{
// TODO: handle exception
if(null!=tx){tx.rollback();}
e.printStackTrace();
}
finally
{
session.close();
sf.close();
}
}
}
如此便大功告成了,欢迎一起学习的同志们多多交流!!!