views:

186

answers:

2

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

Similar to the schemaexporttask with hbm xml files.

+2  A: 

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

Pascal Thivent
+1  A: 

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>);
Arthur Ronald F D Garcia
This looks interesting. Do i need any extra libraries for this ?
Anthony
@Anthony No, It is bundled with Hibernate
Arthur Ronald F D Garcia