Creating database tables from JPA eclipse objects

I want to deploy my Java application in Heroku, I use eclipse link JPA.

To do this, I need to create tables through Java, as long as I have the code below, but how do I get DDL to create the table. I will run the MySQL application for development and Heroku Postgres for production. Therefore, I would 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); } } 
+4
source share
1 answer

OK, I get it.

If I add the following 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 the object, if it is not already in the dataabse, the table will be created.

+12
source

Source: https://habr.com/ru/post/1381200/


All Articles