2011-04-14 45 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:應用程序啓動時,檢查數據庫模式,如果缺少某些表或列,將失敗。