以下代码可以实现生成表:
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 ...{
}
}
}
分享到:
相关推荐
Hibernate运用Annotation由实体类自动在数据库中生成对应的表
Hibernate通过配置文件自动生成表
hibernate自动生成entity,映射文件,dao.等
hibernate自动生成数据库文件.rar hibernate自动生成数据库文件.rar hibernate自动生成数据库文件.rar 不建议下载 某同学自己录的视屏
Hibernate中根据数据库中各表,使用myEclipse的返向工程,自动生成hibernate.cfg.xml 配置文件 与person.hbm.xml映射文件和持久化类 (图文介绍)
hibernate代码自动生成
使用hibernate中的相关配置和方法自动生成数据表和表之间的关系
详解hibernate自动创建表的配置 配置自动创建表: ”hibernate.hbm2ddl.auto”>update</prop>//首次创建项目时用,项目稳定后一般注释这里有4个值: update:表示自动根据model对象来更新表结构,启动hibernate时会...
Hibernate自动生成配置文件(配图)
private static void printTableMetaInfo(Session session) { Connection connection = session.connection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet result = metaData....
hibernate_tools,hibernate_extension.自动生成
在myeclipse中配置hibernate 自动生成配置文件在myeclipse中配置hibernate 自动生成配置文件
Hibernate自动生成文件,可以通过myeclipse自动配置Hibernate文件以及各种Dao包中的增删修改方法
hibernate中自动生成主键的办法
原来我上传过这个资源,但网友说不太完整,我又补充了重新上传。hibernate自动生成dao,配置文件等的学习笔记,在myeclipse下实现。
MyEclipse自动生成hibernate实体类配置文件
自动生成hibernate映射文件和实体类
利用hibernate自动生成hbm.xml文件和entity类,挺详细的文档呢,可以参考看看的喔喔
hibernate自动生成关键映射以及工厂类,连接数据库弄好连接和设置hibernate关键映射了,这些都不用头疼了。直接使用HibernateSessionFactory 工厂类调用方法测试就好了,方便简洁,还能提高编码效率。 本人亲自测试...