5

I want to deploy my Java application to Heroku, I'm using eclipse link JPA.

To do that I need to create the tables via a Java, so far I've got the code below but how do I get the DDL to create a table. I'll be running the app on MySQL for development and Heroku is Postgres for production. Therefore, I'd like the create table statement to take this into account.

import javax.persistence.*;

public class SchemaCreator
{
  public static void main(String[] args)
  {
    EntityManagerFactory factory =
    Persistence.createEntityManagerFactory("bitcoin");
    EntityManager em = factory.createEntityManager();

    String ddl = /// <--- How do I get it ?

    em.createNativeQuery(ddl);
  }
}
Ian Purton
  • 15,331
  • 2
  • 27
  • 26
  • You want to create the DB tables basing on your Entities or generate DDL queries to create the database (and use it somewhere else)? – Piotr Nowicki Nov 14 '11 at 21:51
  • http://stackoverflow.com/questions/779479/reverse-engineer-ddl-from-jpa-entities – madth3 Nov 15 '11 at 01:10
  • I want to be able to create the tables form a Java executable. I don't weant to have to hand code the SQL statements. – Ian Purton Nov 15 '11 at 09:51

1 Answers1

13

OK, I figured it out.

If I add the following entried to my persistence.xml

<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/> 

Then when I first access an entity if it's not already in the dataabse a table will be created.

Ian Purton
  • 15,331
  • 2
  • 27
  • 26
  • Also consider `create-or-extend-tables` ([docs](https://www.eclipse.org/eclipselink/documentation/3.0/jpa/extensions/persistenceproperties_ref.htm#BABHEJJI)) – Jack Oct 30 '20 at 11:15