有没有做定制衣服的网站,爱心代码html简单,网站技术培训班有哪些种类,做文案公众号策划兼职网站maven jpa这种情况很简单–您想要在构建应用程序时生成数据库模式创建脚本#xff08;然后在目标数据库上执行脚本#xff09;#xff0c;这在Hibernate 3中相对容易#xff0c;因为有 hibernate3-maven-plugin #xff0c;但是与Hibernate 4不兼容。当然#xff0c;对于… maven jpa 这种情况很简单–您想要在构建应用程序时生成数据库模式创建脚本然后在目标数据库上执行脚本这在Hibernate 3中相对容易因为有 hibernate3-maven-plugin 但是与Hibernate 4不兼容。当然对于每个新项目都应从Hibernate 4开始。 那么该怎么办 它相对简单但是需要花费一些时间进行研究和测试。 这个想法是使用SchemaExport工具。 但这有点棘手因为它仅支持本地Hibernate配置而不支持JPA。 首先创建一个处理导出的命令行应用程序。 请注意不建议使用Ejb3Configuration但不建议将其用于外部使用-Hibernate在内部大量使用了它。 因此这是一个正常的工作类 SuppressWarnings(deprecation)
public class JpaSchemaExport {public static void main(String[] args) throws IOException {execute(args[0], args[1], Boolean.parseBoolean(args[2]), Boolean.parseBoolean(args[3]));}public static void execute(String persistenceUnitName, String destination, boolean create, boolean format) {System.out.println(Starting schema export);Ejb3Configuration cfg new Ejb3Configuration().configure(persistenceUnitName, new Properties());Configuration hbmcfg cfg.getHibernateConfiguration();SchemaExport schemaExport new SchemaExport(hbmcfg);schemaExport.setOutputFile(destination);schemaExport.setFormat(format);schemaExport.execute(true, false, false, create);System.out.println(Schema exported to destination);}
} 请注意我们没有将文件直接部署到目标数据库。 .execute的第二个参数为false。 这是因为在persistence.xml中没有数据库连接属性-它们是外部的。 稍后在maven构建中完成架构文件的部署但这超出了本文的范围。 然后我们只需要从Maven构建中调用此类。 我最初尝试将其创建为ant任务并使用antrun插件运行它但是它存在类路径和类加载器问题找不到实体和persistence.xml。 这就是为什么我使用exec-maven-plugin的原因该插件在运行构建的同一JVM中调用该应用程序 plugingroupIdorg.codehaus.mojo/groupIdartifactIdexec-maven-plugin/artifactIdversion1.1/versionexecutionsexecutionphase${sql.generation.phase}/phase !-- this is process-classes in our case currently --goalsgoaljava/goal/goals/execution/executionsconfigurationmainClasscom.yourcompany.util.JpaSchemaExport/mainClassargumentsargumentcore/argumentargument${project.build.directory}/classes/schema.sql/argumentargumenttrue/argumentargumenttrue/argument/arguments/configuration
/plugin 然后您可以使用sql-maven-plugin将schema.sql文件部署到目标数据库您将需要由maven加载外部化的db属性这由properties-maven-plugin完成。 参考 如何使用我们的JCG合作伙伴 Bozhidar Bozhanov 来自Bozho的技术博客上的Hibernate 4JPA和Maven生成模式创建脚本 。 翻译自: https://www.javacodegeeks.com/2012/07/schema-creation-script-with-hibernate-4.htmlmaven jpa