2011-04-14 43 views
1

我目前正在使用eclipse jpa工具进行接缝项目;有可能从我的实体定义中自动生成sql表吗?如果是这样,我该如何做到这一点?如何从实体生成表

+0

[本文档](http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jpt.doc.user/tasks020.htm)可能会有所帮助。 – lobster1234 2011-04-14 20:18:30

回答

1

这取决于您正在使用的JPA实现。 使用Hibernate,你可以在persistence.xml指定 'create' 或hibernate.hbm2ddl.auto属性 'update':

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> 
    <persistence-unit name="yourPersistenceUnit" transaction-type="JTA"> 
    <description>Your Persistence Unit</description> 
    <jta-data-source>java:/DefaultDS</jta-data-source> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <properties> 
     <property name="hibernate.hbm2ddl.auto" value="create"/> 
     <property name="hibernate.show_sql" value="true"/> 
     <property name="hibernate.format_sql" value="true"/> 
     <property name="hibernate.transaction.flush_before_completion" value="true"/> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> 
    </properties> 
    </persistence-unit> 
</persistence> 

hibernate.hbm2ddl.auto属性的可能值有:

  • create:在启动时创建
  • 数据库表和索引
  • create-drop:在启动时创建数据库表和索引并在关机时丢弃
  • update:当应用程序启动时,检查数据库模式并根据需要进行更新,添加缺少的表和列
  • validate:应用程序启动时,检查数据库模式,如果缺少某些表或列,将失败。