日期:2014-05-16  浏览次数:20520 次

hibernate创建导出数据库DDL schema

SchemaExport se=new SchemaExport(cfg).setOutputFile("xby.sql");
se.execute(true,true,false,false);

第一个true指导出sql文件,第二个true指执行数据库DDL.

public class HibernateUtils {
	public static final Logger log=LoggerFactory.getLogger(HibernateUtils.class);
	private static SessionFactory sf;
	static{
		try{
			Configuration cfg=new Configuration().configure();
		sf=cfg.buildSessionFactory();
		SchemaExport se=new SchemaExport(cfg).setOutputFile("xby.sql");
		se.execute(true,true,false,false);
		}catch(Throwable e){
			log.error("SessionFacotry init error-{}",e);
			throw new ExceptionInInitializerError(e);
		}
		
	}
	public static SessionFactory getSessionFactory(){
		return sf;
	}
	public static void shutDown(){
		if(sf!=null){
			sf.close();
		}
	}
}