使用hibernate工具类将对象模型生成关系模型

本文介绍了一个使用Hibernate将对象模型转换为数据库表的工具类。该工具通过读取hibernate.cfg.xml配置文件并利用SchemaExport类实现模型到表的映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.wjt276.hibernate;
 
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
 
/**
 * Hibernate工具<br/>
 * 将对象模型生成关系模型(将对象生成数据库中的表)
 * 把hbm映射文件(或Annotation注解)转换成DDL
 * 生成数据表之前要求已经存在数据库
 * 注:这个工具类建立好后,以后就不用建立了。以后直接Copy来用。
 * @author wjt276
 * @version 1.0 2009/10/16
 */
public classExportDB {
    public static voidmain(String[] args){
        /*org.hibernate.cfg.Configuration类的作用:
         *读取hibernate配置文件(hibernate.cfg.xml或hiberante.properties)的.
         *new Configuration()默认是读取hibernate.properties
         *所以使用newConfiguration().configure();来读取hibernate.cfg.xml配置文件
         */
        Configuration cfg = new AnnotationConfiguration().configure();
        /*org.hibernate.tool.hbm2ddl.SchemaExport工具类:
         *需要传入Configuration参数
         *此工具类可以将类导出生成数据库表
         */
        SchemaExport export = newSchemaExport(cfg);
        /** 开始导出
         *第一个参数:script是否打印DDL信息
         *第二个参数:export是否导出到数据库中生成表
         */
        export.create(true, true);
    }}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值