Schema export with hibernate annotations

前端 未结 4 1739
萌比男神i
萌比男神i 2021-02-13 12:35

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

Similar to the schemaexporttask with hbm xml files.

相关标签:
4条回答
  • 2021-02-13 12:53

    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

    • Hibernate 3 Annotations & Ant
    0 讨论(0)
  • 2021-02-13 12:54

    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.

    0 讨论(0)
  • 2021-02-13 13:03

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

    0 讨论(0)
  • 2021-02-13 13:04

    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>);
    
    0 讨论(0)
提交回复
热议问题