带 hibernate 注释的架构导出

发布于 2024-09-12 09:22:33 字数 73 浏览 2 评论 0原文

我正在使用休眠注释,并且想导出我的数据库架构。

与 hbm xml 文件的 schemaexporttask 类似。

I'm using hibernate annotations and i want to export my database schema.

Similar to the schemaexporttask with hbm xml files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小女人ら 2024-09-19 09:22:33

你可以。去做就对了

AnnotationConfiguration configuration = new AnnotationConfiguration();

configuration
.addAnnotatedClass(<TYPE_YOUR_CLASS>.class)
.setProperty(Environment.USER, <TYPE_YOUR_USER>)
.setProperty(Environment.PASS, <TYPE_YOUR_PASSWORD>)
.setProperty(Environment.URL, <TYPE_YOUR_URL>)
.setProperty(Environment.DIALECT, <TYPE_YOUR_DIALECT>)
.setProperty(Environment.DRIVER, <TYPE_YOUR_DRIVER>);

SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("schema.sql");

schema.create(<DO_YOU_WANT_TO_PRINT_TO_THE_CONSOLE>, <DO_YOU_WANT_TO_EXPORT_THE_SCRIPT_TO_THE_DATABASE>);

You can. Just do it

AnnotationConfiguration configuration = new AnnotationConfiguration();

configuration
.addAnnotatedClass(<TYPE_YOUR_CLASS>.class)
.setProperty(Environment.USER, <TYPE_YOUR_USER>)
.setProperty(Environment.PASS, <TYPE_YOUR_PASSWORD>)
.setProperty(Environment.URL, <TYPE_YOUR_URL>)
.setProperty(Environment.DIALECT, <TYPE_YOUR_DIALECT>)
.setProperty(Environment.DRIVER, <TYPE_YOUR_DRIVER>);

SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("schema.sql");

schema.create(<DO_YOU_WANT_TO_PRINT_TO_THE_CONSOLE>, <DO_YOU_WANT_TO_EXPORT_THE_SCRIPT_TO_THE_DATABASE>);
坏尐絯℡ 2024-09-19 09:22:33

事实上,原始的 Hibernate Core SchemaExportTask 只能处理 Hibernate XML 映射文件,而不能处理注释。您需要的是 Hibernate Tools 附带的 HibernateToolTask​​

以下是改编自 Java Persistence With Hibernate 的用法示例:

<taskdef name="hibernatetool"
         classname="org.hibernate.tool.ant.HibernateToolTask"
         classpathref="project.classpath"/>
  <target name="schemaexport" depends="compile, copymetafiles"
          description="Exports a generated schema to DB and file">
    <hibernatetool destdir="${basedir}">
      <classpath path="${build.dir}"/>
      <configuration 
          configurationfile="${build.dir}/hibernate.cfg.xml"/>
      <hbm2ddl
          drop="true"
          create="true"
          export="true"
          outputfilename="helloworld-ddl.sql"
          delimiter=";"
          format="true"/>
    </hibernatetool>
</target>

另请参阅

Indeed, the original Hibernate Core SchemaExportTask can only handle Hibernate XML mapping files, not annotations. What you need is the HibernateToolTask that comes with Hibernate Tools.

Here is an Usage example adapted from Java Persistence With Hibernate:

<taskdef name="hibernatetool"
         classname="org.hibernate.tool.ant.HibernateToolTask"
         classpathref="project.classpath"/>
  <target name="schemaexport" depends="compile, copymetafiles"
          description="Exports a generated schema to DB and file">
    <hibernatetool destdir="${basedir}">
      <classpath path="${build.dir}"/>
      <configuration 
          configurationfile="${build.dir}/hibernate.cfg.xml"/>
      <hbm2ddl
          drop="true"
          create="true"
          export="true"
          outputfilename="helloworld-ddl.sql"
          delimiter=";"
          format="true"/>
    </hibernatetool>
</target>

See also

楠木可依 2024-09-19 09:22:33

如果有人感兴趣如何通过单元测试使用 JPA+Spring 执行此操作(您可以轻松地从 Eclipse 内部生成运行单元测试的 sql):

ExportDatabaseSchema.java:


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration(defaultRollback=true)
public class ExportDatabaseSchema {

  @Resource(name="&entityManagerFactory")
  private LocalContainerEntityManagerFactoryBean entityManagerFactory;

  @Test
  public void exportDatabaseSchema(){
    PersistenceUnitInfo persistenceUnitInfo = entityManagerFactory.getPersistenceUnitInfo();
    Map jpaPropertyMap = entityManagerFactory.getJpaPropertyMap();

    Configuration configuration = new Ejb3Configuration().configure( persistenceUnitInfo, jpaPropertyMap ).getHibernateConfiguration();

    SchemaExport schema = new SchemaExport(configuration);
    schema.setOutputFile("resources/sql/schema.sql");
    schema.create(false, false);
  }
}

您需要一个 ExportDatabaseSchema-context.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="classpath:applicationContext-jpa.xml" /> 
</beans>

applicationContext -jpa.xml 包含配置的entityManagerFactory bean 的注释。诀窍是用 & 注入它像这样:“&entityManagerFactory”,取消引用 spring 生成的代理。

In case someone is interested how to do this with JPA+Spring from a unit test (you can generate the sql running the unit test from inside Eclipse like a breeze):

ExportDatabaseSchema.java:


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration(defaultRollback=true)
public class ExportDatabaseSchema {

  @Resource(name="&entityManagerFactory")
  private LocalContainerEntityManagerFactoryBean entityManagerFactory;

  @Test
  public void exportDatabaseSchema(){
    PersistenceUnitInfo persistenceUnitInfo = entityManagerFactory.getPersistenceUnitInfo();
    Map jpaPropertyMap = entityManagerFactory.getJpaPropertyMap();

    Configuration configuration = new Ejb3Configuration().configure( persistenceUnitInfo, jpaPropertyMap ).getHibernateConfiguration();

    SchemaExport schema = new SchemaExport(configuration);
    schema.setOutputFile("resources/sql/schema.sql");
    schema.create(false, false);
  }
}

You need an ExportDatabaseSchema-context.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="classpath:applicationContext-jpa.xml" /> 
</beans>

The applicationContext-jpa.xml contains the annotation configured entityManagerFactory bean. The trick is to inject it with & like this: "&entityManagerFactory", to dereference the spring generated proxy.

喵星人汪星人 2024-09-19 09:22:33

使用 hibernate3-maven-plugin。然后运行“mvn hibernate3:hbm2ddl”

Use hibernate3-maven-plugin. Then run 'mvn hibernate3:hbm2ddl'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文