- 当在设计时,我们的领域模型需要改变,只需修改NHibernate结构和应用程序,不需要修改数据库架构,只要利用SchemaExport工具重新生成数据库架构就可以了。
- SchemaExport工具就是把DDL脚本输出到标准输出,同时/或者执行DDL语句。SchemaExport工具提供了三个方法,分别是Drop()、Create()、Execute(),前两个方法实质是调用Execute()方法。通常使用Execute()方法来生成数据库架构的。
package com.test;
import java.util.EnumSet;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import com.been.Address;
import com.been.Student;
import com.util.GetSessionFactory;
public class test1 {
public static void main(String[] args) {
//add();
buile();
}
public static void buile(){
ServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(registry).buildMetadata();
SchemaExport export = new SchemaExport();
export.create(EnumSet.of(TargetType.DATABASE),metadata);
}
}
本文介绍如何使用NHibernate的SchemaExport工具来自动生成或更新数据库架构。通过简单的Java代码示例,展示了如何配置并利用SchemaExport工具执行DDL语句,实现数据库的自动化管理。
1728

被折叠的 条评论
为什么被折叠?



