回答
從數據庫導出數據,SQL
使用liquibase開源項目
LiquiBase是一個開源(LGPL),用於跟蹤,管理和應用數據庫更改數據庫無關的庫。它是建立在一個簡單的前提:所有數據庫的變化(結構和數據)被存儲在基於XML的描述性方式和檢入源控制。
生成創建和給定的JPA實體
我們使用此代碼生成下降和創建報表下降腳本: 就構建這個類的所有實體類,並調用創建/ dropTableScript。
如果需要,你可以使用一個persitence.xml和persitance單位名稱代替。只要說一下 ,我也會發布代碼。
import java.util.Collection; import java.util.Properties; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.dialect.Dialect; import org.hibernate.ejb.Ejb3Configuration; /** * SQL Creator for Tables according to JPA/Hibernate annotations. * * Use: * * {@link #createTablesScript()} To create the table creationg script * * {@link #dropTablesScript()} to create the table destruction script * */ public class SqlTableCreator { private final AnnotationConfiguration hibernateConfiguration; private final Properties dialectProps; public SqlTableCreator(final Collection> entities) { final Ejb3Configuration ejb3Configuration = new Ejb3Configuration(); for (final Class entity : entities) { ejb3Configuration.addAnnotatedClass(entity); } dialectProps = new Properties(); dialectProps.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect"); hibernateConfiguration = ejb3Configuration.getHibernateConfiguration(); } /** * Create the SQL script to create all tables. * * @return A {@link String} representing the SQL script. */ public String createTablesScript() { final StringBuilder script = new StringBuilder(); final String[] creationScript = hibernateConfiguration.generateSchemaCreationScript(Dialect .getDialect(dialectProps)); for (final String string : creationScript) { script.append(string).append(";\n"); } script.append("\ngo\n\n"); return script.toString(); } /** * Create the SQL script to drop all tables. * * @return A {@link String} representing the SQL script. */ public String dropTablesScript() { final StringBuilder script = new StringBuilder(); final String[] creationScript = hibernateConfiguration.generateDropSchemaScript(Dialect .getDialect(dialectProps)); for (final String string : creationScript) { script.append(string).append(";\n"); } script.append("\ngo\n\n"); return script.toString(); } }
感謝您的回答,是否可以使用persistence.xml? – 2009-06-30 17:45:56
Hibernate內置了對此的支持。見org.hibernate.tool.hbm2ddl.SchemaExport。
OpenJPA的支持這個功能了。 OpenJPA mapping tool可以創建腳本或創建ddl文件。 ddl應該與其他JPA實現一起工作(儘管每個供應商都有一些怪癖)。
如果您正在使用的OpenJPA作爲持久性提供可配置的OpenJPA創建表他們通過將SynchronizeMappings屬性persistence.xml中所需要的第一次。
實施例:
<persistence-unit name="test">
<!--
. . .
-->
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema"/>
</properties>
<!--
. . .
-->
</persistence-unit>
DataNucleus將具有SchemaTool可以調用從Java,或從命令行。它做什麼,你需要
--Andy(DataNucleus)
這裏有一個如何使用Hibernate的SchemaExport類做你想要什麼的交代。
http://jandrewthompson.blogspot.com/2009/10/how-to-generate-ddl-scripts-from.html
希望這有助於。
- 1. 如何讓Java JPA創建實體?
- 2. 逆向工程從JPA實體DDL
- 3. JPA:每當創建另一個實體時創建一個實體
- 4. CoreData:創建一個新實體作爲舊實體的父代
- 5. 使用Jersey從一個JSON創建多個JPA持久實體?
- 6. 如何自我加入實體以使用JPA創建一組實體?
- 7. JPA - 如何DDL
- 8. JPA 2.1在JPA EntityListener中創建實體
- 9. 如何從java代碼創建log4j2 appender?
- 10. Java hibernate/jpa如何創建自我相關的動態通用實體
- 11. 如何創建一個新的實體
- 12. 如何創建Java代碼
- 13. 如何創建Java代碼
- 14. 如何使用JPA(Hibernate)創建與同一實體的關係?
- 15. 創建一個新表實體框架代碼第一模式
- 16. 如何阻止eclipse從表中自動創建JPA實體?
- 17. 實體框架代碼第一:如何爲這種關係創建一對多?
- 18. 如何爲實體創建多個表?
- 19. 我的Java代碼顯示一個編譯器錯誤,當我爲我上面的Java代碼創建方法
- 20. 如何爲JPA實體類禁用SQL創建
- 21. JPA OneToOne和OneToMany實體實例/創建
- 22. 我如何爲這段代碼創建一個頭文件?
- 23. 如何使用嵌入的JPA和JavaDB從Java代碼創建數據庫表?
- 24. 如何創建一個實體和org.hibernate.usertype.UserType
- 25. 爲Java代碼創建UML
- 26. 爲Java代碼創建MSI
- 27. 從數據庫爲JPA實體自定義代碼生成
- 28. JPA /休眠爲DDL
- 29. 從java代碼創建多個httpServletRequest
- 30. java代碼創建一個組合的唯一代碼
這是http://stackoverflow.com/questions/297438/auto-generate-data-schema-from-jpa-annotated-entity-classes重演。 – stevedbrown 2009-06-30 18:07:21