2

I am trying to export the generated DDL to a output file instead of directly showing in command prompt for hbm2ddl.auto=update(using automatic schema generation).

I know this may not be good idea to generate the schema using this for production database. I have already gone thru the threads that talk about it.

Hibernate: hbm2ddl.auto=update in production?

I am trying use this for development/test databases.

But, the challenge I am facing here is it's not sending out the generated DDL to a output file even though I am using the "outputfilename" attribute in hbm2ddl
exporter. (as per this http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html/ant.html)

Here is how my build.xml looks like. I am using the Hibernate's Jpa configuration.

<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"     classpathref="classpath.hibernate" />

<target name="db_update_schema_sql" depends="db_config"
        description="create SQL script for updating schema">
    <hibernatetool destdir="${out.dir}">
       <jpaconfiguration persistenceunit="${persistence.unit.name}"/>
       <classpath path="${build.classes.dir}"/>             
       <hbm2ddl export="false" create="false" update="true" drop="false"     outputfilename="update.sql" delimiter=";" format="true"/>          
    </hibernatetool>
</target>

<target name="db_full_update" depends="db_config" description="Updates database">
    <antcall target="db_update_schema_sql" />
</target>

The "update.sql" is never created/updated when I set the update="true". It only creates/update when I use either create="true" or drop="true" or both.

I digged little bit into this. I found couple of issues related to this in Hibernate Jira. It still says it's unresolved. There is a patch available for this. Not sure
whether someone used this patch.

https://hibernate.onjira.com/browse/HHH-1186 https://hibernate.onjira.com/browse/HBX-757

I would really appreciate if someone using this update="true" feature to generate the ddl directly to a file explain to me how they have done it.

Also, if you use the patch for this, can you let me know how you applied the patch to the existing Jar file.

Thanks, SM

Community
  • 1
  • 1
mishra
  • 21
  • 3

1 Answers1

0

Just use the relevant classes directly.

Connection connection = DriverManager.getConnection( dbUrl, dbUsername, dbPassword );
connection.setSchema (dbSchema);
Dialect dialect = ...; // eg, new MySQL5InnoDBDialect();
DatabaseMetadata metadata = new DatabaseMetadata( connection, dialect, null );

Configuration cfg = new Configuration();
cfg.addAnnotatedClass(...); // or read from xml
cfg.buildMappings();
List<SchemaUpdateScript> updateScritps = cfg.generateSchemaUpdateScriptList( dialect, metadata );

for ( SchemaUpdateScript script : updateScritps ) 
{
    String formatted = FormatStyle.DDL.getFormatter().format (script.getScript());

    // Replace with writing to file:
    System.out.println( formatted + ";" );
}

Programmers are meant to program, not beat their heads against XML.

Also check out the source code of org.hibernate.tool.hbm2ddl.SchemaUpdate.

Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99