1 Reply Latest reply on Dec 19, 2008 9:15 PM by Stephen Friedrich

    SchemaExport

    Andre de Fontana Ignacio Newbie

      Hi all..



      Is It possible programmatic schema export? How?


      I tried:


      Ejb3Configuration cfg = new Ejb3Configuration ().configure ( "/META-INF/persistence.xml" );
      final Configuration hbmcfg = cfg.getHibernateConfiguration ();
      final SchemaExport schemaExport = new SchemaExport ( hbmcfg );
      schemaExport.create ( true, true );
      



      But doesn't work...


      Error message: invalid configuration: Document is invalid: no grammar found.


      The persitence file path is correct.


      Thanks

        • 1. Re: SchemaExport
          Stephen Friedrich Novice

          Here's what I use for my annotated entities. Not very elegant, because you need to specify each entity separately, but I haven't found another way yet (but didn't search too hard either).



              public static void main(String[] args) {
                  AnnotationConfiguration configuration = new AnnotationConfiguration();
          
                  // Select the dialect matching the selected DBMSs
          
                  //configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
                  configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect");
          
                  // Add all root packages that contain entities
                  configuration.addPackage("com.acme.foo.model");
          
                  // Add each entity
                  configuration.addAnnotatedClass(ContactData.class);
                  // ...
                  configuration.addAnnotatedClass(User.class);
          
                  // Dump formated SQL to console and file
                  SchemaExport schemaExport = new SchemaExport(configuration);
                  schemaExport.setOutputFile("schema.sql");
                  schemaExport.setFormat(true);
                  schemaExport.setDelimiter(";");
                  schemaExport.execute(true, // write schema to output
                                       false, // do not export to DB
                                       false, // no drop statements
                                       true// only create statements
                  );
              }