Hibernate中利用配置文件(hbm)自动生成数据库表

Hibernate中利用配置文件(hbm)自动生成数据库表

自动通过hbm文件自动建立数据库表 。以类为基础,生成配置文件和数据库表,更加符合OO。
上一篇文章自动生成了Position.hbm.xml和Users.hbm.xml两个配置文件,将其加入hibernate.cfg.xml中,然后建立HibernateSchemaExport类,代码如下:
Java代码

1. package test;
2.
3. import java.io.File;
4.
5. import org.hibernate.HibernateException;
6. import org.hibernate.Session;
7. import org.hibernate.SessionFactory;
8. import org.hibernate.Transaction;
9. import org.hibernate.cfg.Configuration;
10. import org.hibernate.tool.hbm2ddl.SchemaExport;
11.
12. public class HibernateSchemaExport ...{
13.
14. static Session session;
15.
16. static Configuration config = null;
17. static Transaction tx = null;
18.
19. public static void main(String[] args) ...{
20. /** *//**
21. * 根据映射文件创建数据库结构
22. */
23. try ...{
24. config = new Configuration().configure(new File(
25. "src/hibernate.cfg.xml"));
26.
27. System.out.println("Creating tables...");
28.
29. SessionFactory sessionFactory = config.buildSessionFactory();
30. session = sessionFactory.openSession();
31. tx = session.beginTransaction();
32.
33. SchemaExport schemaExport = new SchemaExport(config);
34. schemaExport.create(true, true);
35.
36. System.out.println("Table created.");
37.
38. tx.commit();
39.
40. } catch (HibernateException e) ...{
41. e.printStackTrace();
42. try ...{
43. tx.rollback();
44. } catch (HibernateException e1) ...{
45. e1.printStackTrace();
46. }
47. } finally ...{
48.
49. }
50. }
51.
52. }

package test;

import java.io.File;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class HibernateSchemaExport ...{

static Session session;

static Configuration config = null;
static Transaction tx = null;

public static void main(String[] args) ...{
/** *//**
* 根据映射文件创建数据库结构
*/
try ...{
config = new Configuration().configure(new File(
"src/hibernate.cfg.xml"));

System.out.println("Creating tables...");

SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();

SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);

System.out.println("Table created.");

tx.commit();

} catch (HibernateException e) ...{
e.printStackTrace();
try ...{
tx.rollback();
} catch (HibernateException e1) ...{
e1.printStackTrace();
}
} finally ...{

}
}

}



运行,出现如下输出:
Sql代码

1. Creating tables...
2. alter table test_user_position drop foreign key FKF1F5A2301D5E879B
3. alter table test_user_position drop foreign key FKF1F5A2307D008B16
4. drop table if exists test_position
5. drop table if exists test_user_position
6. drop table if exists test_uses
7. create table test_position (
8. id integer not null auto_increment,
9. name integer,
10. primary key (id)
11. )
12. create table test_user_position (
13. position_id integer not null,
14. user_id integer not null,
15. primary key (user_id, position_id)
16. )
17. create table test_uses (
18. id integer not null auto_increment,
19. name varchar(25),
20. primary key (id)
21. )
22. alter table test_user_position
23. add index FKF1F5A2301D5E879B (user_id),
24. add constraint FKF1F5A2301D5E879B
25. foreign key (user_id)
26. references test_uses (id)
27. alter table test_user_position
28. add index FKF1F5A2307D008B16 (position_id),
29. add constraint FKF1F5A2307D008B16
30. foreign key (position_id)
31. references test_position (id)
32. Table created.

Creating tables...
alter table test_user_position drop foreign key FKF1F5A2301D5E879B
alter table test_user_position drop foreign key FKF1F5A2307D008B16
drop table if exists test_position
drop table if exists test_user_position
drop table if exists test_uses
create table test_position (
id integer not null auto_increment,
name integer,
primary key (id)
)
create table test_user_position (
position_id integer not null,
user_id integer not null,
primary key (user_id, position_id)
)
create table test_uses (
id integer not null auto_increment,
name varchar(25),
primary key (id)
)
alter table test_user_position
add index FKF1F5A2301D5E879B (user_id),
add constraint FKF1F5A2301D5E879B
foreign key (user_id)
references test_uses (id)
alter table test_user_position
add index FKF1F5A2307D008B16 (position_id),
add constraint FKF1F5A2307D008B16
foreign key (position_id)
references test_position (id)
Table created.



现在看看数据库,已经成功地创建了test_position、 test_uses和 test_user_position 三张表。


[b]hibenate.hbm2ddl.auto属性详解[/b]
hibernate 配置属性中,hibernate.hbm2ddl.auto可以帮助你实现正向工程,即由java代码生成数据库脚本,进而生成具体的表结构.
&在hibernate.cfg.xml中:
Java代码 复制代码

1. <property name="hibernate.hbm2ddl.auto">
2. </property>

<property name="hibernate.hbm2ddl.auto">
</property>


它包含4个属性:

* create : 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成表,哪怕2次没有任何改变


* create-drop : 根据model类生成表,但是sessionFactory一关闭,表就自动删除


* update : 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行


* validate : 只会和数据库中的表进行比较,不会创建新表,但是会插入新值


在hibernate中,如果在hibernate.cfg.xml文件中,将hibernate.hbm2ddl.auto设置为update(或者cretae-drop)也可以,如下
<property name="hibernate.hbm2ddl.auto">update</property>
则在运行应用程序时(第一次),会自动建立起表的结构(前提是先建立好数据库)。要注意的是,
当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会

如果是spring配置文件中配置则为
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>

<prop key="hibernate.connection.autocommit">true</prop>

<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值