Hibernate自动生成表

开发 后端
本篇文章提供了可以实现Hibernate自动生成表的代码。

以下代码可以实现Hibernate自动生成表:

  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(truetrue);     
  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. }  

【编辑推荐】

  1. Hibernate的三个状态(1)
  2. 扩展hibernate的查询
  3. 简单的Hibernate入门介绍
  4. Hibernate的三个状态(3)
  5. Hibernate的三个状态(2)

 

责任编辑:book05 来源: 百度博客
相关推荐

2009-09-24 16:56:12

Hibernate S

2009-09-22 09:31:15

Hibernate主键

2009-09-24 10:07:21

Hibernate M

2009-09-25 13:33:43

Hibernate主键

2009-06-25 17:24:06

Hibernate主键

2009-06-29 08:50:20

Hibernate主键

2009-06-19 14:45:28

Hibernate S

2010-10-19 15:31:40

sql server触

2009-09-28 10:52:33

Hibernate主键主键生成

2009-09-24 16:22:50

Hibernate自动

2009-06-29 08:56:49

Hibernate主键生成主键

2009-09-22 13:25:54

Hibernate M

2009-07-16 11:21:19

ibatis主键自动生成

2009-06-26 16:21:22

Oracle自动增长Hibernate

2012-02-09 13:31:03

HibernateJava

2009-07-14 18:24:31

ibatis映射文件

2009-09-25 11:23:13

Hibernate S

2009-06-18 14:22:06

Hibernate多对Hibernate

2011-09-02 16:21:08

Sencha Touc自动生成工具

2009-07-16 09:09:36

ibatis自动代码
点赞
收藏

51CTO技术栈公众号