Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create database tables from eclipse link JPA entities

Tags:

java

heroku

jpa

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);
  }
}
like image 570
Ian Purton Avatar asked Nov 14 '11 21:11

Ian Purton


People also ask

How will you create a table from JPA entities?

Right-click the JPA project in the Project Explorer and select JPA Tools > Generate Entities from Tables. On the Select Tables page of the Generate Entities from Tables wizard, select your database connection and schema. To create a new database connection, click Add connection.

Is EclipseLink an ORM?

EclipseLink Native Object Relational Mapping (ORM) provides an extensible object-relational mapping framework. It provides high-performance object persistence with extended capabilities configured declaratively through XML.

Is EclipseLink a framework?

Eclipse Persistence Services Project (EclipseLink) is a comprehensive persistence framework delivering a set of persistence services based around leading standards with advanced extensions. Consumers can use EclipseLink within Java EE, SE, and OSGi/Equinox environments.


1 Answers

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.

like image 88
Ian Purton Avatar answered Nov 04 '22 00:11

Ian Purton