2011-11-14 96 views
4

我想将我的Java应用程序部署到Heroku,我使用的是eclipse链接JPA。从eclipse链接创建数据库链接JPA实体

要做到这一点,我需要通过Java创建表,到目前为止我已经得到了下面的代码,但是如何获得DDL来创建表。我将在MySQL上运行应用程序进行开发,Heroku是Postgres for production。因此,我希望create table语句考虑到这一点。

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); 
    } 
} 
+0

你想创建基于你的实体的数据库表或生成DDL查询来创建数据库(并在别处使用它)? –

+0

http://stackoverflow.com/questions/779479/reverse-engineer-ddl-from-jpa-entities – madth3

+0

我想能够创建表格形式的Java可执行文件。我不必手动编写SQL语句。 –

回答

12

好的,我想通了。

如果我添加以下entried我的persistence.xml

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

后来,当我第一次访问实体,如果它不是已经在一个表将要创建的dataabse。