在MyEclipse中使用Hibernate的正向工程

6 篇文章 0 订阅
2 篇文章 0 订阅

        我们一般用Hibernate中的反向工程比较多,即通过现有的表产生实体类和映射文件。因为按照软件的开发过程,一般是先做数据库的,后写源代码。但有的时候我们已经有源代码和映射文件了,却没有表结构。现在我们来用用Hibernate的正向工程。myeclipse使用hibernate正向工程,hibernate的正向工程是指从java实体类生成数据库表的过程,具体实施过程如下。

  1. 假设现在已经有实体类
  2. myeclipst项目名称右击myeclipse,点击run xdoclet,如果没有进行myclipse xdoclet配置,需要先进行配置才能执行上述操作。具体配置方法为,项目选中右击点properties选myclipse—>xdoclet选add standard在弹出来的对话框中选standard hibernate.
  3. 运行完xdoclet之后将会生成hibernate映射文件,然后在hibernate配置文件中配置好数据库连接设置hibernate.hbm2ddl.auto 为create重新启动tomcat就会生成表。

另附上hibernate.hbm2ddl.auto的参数作用:

create
Hibernate will create the database when the entity manager factory is created (actually when Hibernate's SessionFactory is created by the entity manager factory). If a file named import.sql exists in the root of the class path ('/import.sql') Hibernate will execute the SQL statements read from the file after the creation of the database schema. It is important to remember that before Hibernate creates the schema it empties it (delete all tables, constraints, or any other database object that is going to be created in the process of building the schema).

create-drop
Same as 'create' but when the entity manager factory (which holds the SessionFactory) is explicitly closed the schema will be dropped.

update
Hibernate creates an update script trying to update the database structure to the current mapping. Does not read and invoke the SQL statements from import.sql. Useful, but we have to be careful, not all of the updates can be done performed ? for example adding a not null column to a table with existing data.

validate
Validates the existing schema with the current entities configuration. When using this mode Hibernate will not do any changes to the schema and will not use the import.sql file.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值