-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: generateDDL with LocalContainerEntityManagerFactoryBean
PostPosted: Sun Mar 04, 2012 6:25 am 
Beginner
Beginner

Joined: Sat Sep 12, 2009 10:20 am
Posts: 44
I'm using the packagesToScan option so that I don't have to maintain a persistence.xml.

It is possible to build a utility that would generate the ddl commands and write them to disc?

Hacking away based on a couple forum posts, I have this, but it doesn't work and also it involves some deprecated classes.


Code:
      BasicConfigurator.configure();
      System.setProperty("our.hibernate.hbm2ddl.auto", "none");
      ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/mockApplicationContext.xml");

      Object fo = ctx.getBean("entityManagerFactory");
      LocalContainerEntityManagerFactoryBean factory = (LocalContainerEntityManagerFactoryBean) fo;
      
         Ejb3Configuration ejb3 = new Ejb3Configuration();
         ejb3.configure(factory.getPersistenceUnitInfo(),
               factory.getJpaPropertyMap());
         Configuration config = ejb3.getHibernateConfiguration();
         config.buildMappings();
         //AuditConfiguration.getFor(config); // include audit tables
         Dialect dialect = Dialect.getDialect(config.getProperties());
         DatabaseMetadata metadata;
         Connection conn = null;
         try {
            EntityManager em = factory.getObject().createEntityManager();
            conn = factory.getDataSource().getConnection();
            metadata = new DatabaseMetadata(conn, dialect);
            String[] scripts = config.generateSchemaUpdateScript(dialect,
                  metadata);
            // String[] scripts = config.generateSchemaCreationScript(dialect);
            // String[] scripts = config.generateDropSchemaScript(dialect);
            for (String script : scripts) {
               System.out.println(script);
            }
         } catch (Throwable t) {
            t.printStackTrace();
         } finally {
            if (conn != null) {
               try {
                  conn.close();
               } catch (Throwable t) {
                  t.printStackTrace();
               }
            }
         }
      }   


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.