我正在使用JPA(Hibernate的实现)来注释实体类以持久化到关系数据库(MySQL或SQL Server).是否有一种简单的方法可以从带注释的类中自动生成数据库模式(表创建脚本)?
我仍处于原型设计阶段,预计会频繁更改架构.我希望能够从带注释的代码中指定和更改数据模型.Grails类似于它从域类生成数据库.
你可以使用Hibernate的hbm2ddl.文档在这里.
为给定的JPA实体生成创建和删除脚本
我们使用此代码生成drop和create语句:只需使用所有实体类构造此类并调用create/dropTableScript.
如果需要,您可以使用persitence.xml和persitance单元名称.只是说点什么,我也发布了代码.
import java.util.Collection; import java.util.Properties; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.dialect.Dialect; import org.hibernate.ejb.Ejb3Configuration; /** * SQL Creator for Tables according to JPA/Hibernate annotations. * * Use: * * {@link #createTablesScript()} To create the table creationg script * * {@link #dropTablesScript()} to create the table destruction script * */ public class SqlTableCreator { private final AnnotationConfiguration hibernateConfiguration; private final Properties dialectProps; public SqlTableCreator(final Collection> entities) { final Ejb3Configuration ejb3Configuration = new Ejb3Configuration(); for (final Class> entity : entities) { ejb3Configuration.addAnnotatedClass(entity); } dialectProps = new Properties(); dialectProps.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect"); hibernateConfiguration = ejb3Configuration.getHibernateConfiguration(); } /** * Create the SQL script to create all tables. * * @return A {@link String} representing the SQL script. */ public String createTablesScript() { final StringBuilder script = new StringBuilder(); final String[] creationScript = hibernateConfiguration.generateSchemaCreationScript(Dialect .getDialect(dialectProps)); for (final String string : creationScript) { script.append(string).append(";\n"); } script.append("\ngo\n\n"); return script.toString(); } /** * Create the SQL script to drop all tables. * * @return A {@link String} representing the SQL script. */ public String dropTablesScript() { final StringBuilder script = new StringBuilder(); final String[] creationScript = hibernateConfiguration.generateDropSchemaScript(Dialect .getDialect(dialectProps)); for (final String string : creationScript) { script.append(string).append(";\n"); } script.append("\ngo\n\n"); return script.toString(); } }
作为相关说明:可以在此处找到使用EclipseLink JPA生成数据库模式的文档.
由于Hibernate 4.3+现在实现了JPA 2.1,因此生成DDL脚本的适当方法是使用以下一组JPA 2.1属性:
由于它将在运行时运行,您可能希望在构建时执行此DDL生成. Hibernate4不再支持官方maven插件,因为Hibernate团队正在转向Gradle.
无论如何,这是以编程方式生成此脚本的JPA 2.1方法:
import java.io.IOException; import java.util.Properties; import javax.persistence.Persistence; import org.hibernate.jpa.AvailableSettings; public class JpaSchemaExport { public static void main(String[] args) throws IOException { execute(args[0], args[1]); System.exit(0); } public static void execute(String persistenceUnitName, String destination) { System.out.println("Generating DDL create script to : " + destination); final Properties persistenceProperties = new Properties(); // XXX force persistence properties : remove database target persistenceProperties.setProperty(org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO, ""); persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "none"); // XXX force persistence properties : define create script target from metadata to destination // persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SCHEMAS, "true"); persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION, "create"); persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata"); persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET, destination); Persistence.generateSchema(persistenceUnitName, persistenceProperties); } }
你可以看到它非常简单!
您现在可以在AntTask或MAVEN这样的构建中使用它(对于MAVEN):
maven-antrun-plugin 1.7 generate-ddl-create process-classes run
以下是如何使用hibernate SchemaExport类来完成您想要的操作的解释.
http://jandrewthompson.blogspot.com/2009/10/how-to-generate-ddl-scripts-from.html