java 从 JPA 实体逆向工程 DDL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/779479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse engineer DDL from JPA entities
提问by Gaurav
I'm playing around with some JPA stuff, changing the mappings to see how they're supposed to be etc. It's basic experimentation. However I can't find a tool that will simply read my entities and then generate the table schema for me. I tried to find something like this in JBoss tools but nada. Eclipse integration will be a huge plus but i'll take a command line tool or an ant task.
我正在玩一些 JPA 的东西,更改映射以查看它们应该如何等等。这是基本的实验。但是,我找不到可以简单地读取我的实体然后为我生成表模式的工具。我试图在 JBoss 工具中找到类似的东西,但 nada。Eclipse 集成将是一个巨大的优势,但我会使用命令行工具或 ant 任务。
Any ideas?
有任何想法吗?
采纳答案by andri
I don't think there is an universal way of doing this with JPA, you have to directly use the underlying JPA implementation to achieve this.
我不认为使用 JPA 有一种通用的方法可以做到这一点,您必须直接使用底层的 JPA 实现来实现这一点。
For Hibernate, there are several possibilities:
对于Hibernate,有几种可能性:
- Use the method duffymoposted earlier, that makes Hibernate update the database schema automatically.
- If you do not want that, you can use the hbm2ddltool from Hibernate Tools(note: link sucks, but apparently their home page is a bit broken right now). You can use that to automatically generate database creation scripts from your JPA entities; there are also plugins for Maven and Ant that invoke
hbm2ddlautomatically.
- 使用之前发布的duffymo方法,它使 Hibernate 自动更新数据库模式。
- 如果你不希望出现这种情况,你可以使用就是hbm2ddl从工具的Hibernate工具(注:链接很烂,但显然他们的主页是有点现在打破)。您可以使用它从 JPA 实体自动生成数据库创建脚本;还有
hbm2ddl自动调用的 Maven 和 Ant 插件。
For EclipseLink(formerly Oracle TopLink, the JPA 2.0 RI) see Using EclipseLink JPA Extensions for Schema Generation. In principle it is very similar to Hibernate, although at first glance I don't see anything that could be used as a stand-alone utility for creating a DB script.
对于EclipseLink(以前称为 Oracle TopLink,JPA 2.0 RI),请参阅Using EclipseLink JPA Extensions for Schema Generation。原则上它与 Hibernate 非常相似,尽管乍一看我没有看到任何可以用作创建 DB 脚本的独立实用程序的东西。
Other JPA implementations (BEA/Oracle Kodo, Apache OpenJPA) probably have their own specific methods of achieving this.
其他 JPA 实现(BEA/Oracle Kodo、Apache OpenJPA)可能有自己的特定方法来实现这一点。
回答by James McMahon
Try adding the following to your persistence.xml
尝试将以下内容添加到您的persistence.xml
For Hibernate:
对于休眠:
To create:
去创造:
<property name="hibernate.hbm2ddl.auto" value="update"/>
To drop and create:
删除和创建:
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
For Toplink:
对于顶级链接:
To create:
去创造:
<property name="toplink.ddl-generation" value="create-tables"/>
To drop and create:
删除和创建:
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
For EclipseLink:
对于 EclipseLink:
To create:
去创造:
<property name="eclipselink.ddl-generation" value="create-tables"/>
To drop and create:
删除和创建:
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
回答by duffymo
When I use Hibernate I simply add this to my config file:
当我使用 Hibernate 时,我只需将其添加到我的配置文件中:
<property name="hbm2ddl.auto">update</property>
Takes care of everything. I'm not sure what the equivalent is for JPA, but it's so heavily influenced by Hibernate that I'd be surprised if you couldn't find something similar.
照顾一切。我不确定 JPA 的等价物是什么,但它受 Hibernate 的影响如此之大,如果您找不到类似的东西,我会感到惊讶。
No tool needed. I usually just run a simple JUnit test and the database is created for me.
不需要工具。我通常只运行一个简单的 JUnit 测试,然后为我创建数据库。
回答by sendon1982
By maven plugin:
通过 maven 插件:
<plugin>
<!-- run "mvn hibernate3:hbm2ddl" to generate a schema -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>3.0</version>
<configuration>
<hibernatetool>
<classpath>
<path location="${project.build.directory}/classes" />
<path location="${project.basedir}/src/main/resources/META-INF/" />
</classpath>
<jpaconfiguration persistenceunit="galleryPersistenceUnit" />
<hbm2ddl create="true" export="false" destdir="${project.basedir}/target" drop="true" outputfilename="mysql.sql" format="true" console="true"/>
</hibernatetool>
</configuration>
</plugin>
回答by Randy Stegbauer
I use Hibernate's org.hibernate.tool.hbm2ddl.SchemaExportclass and this small method to
我使用 Hibernate 的org.hibernate.tool.hbm2ddl.SchemaExport类和这个小方法来
generate the schema in the database:
在数据库中生成模式:
public static void rebuildSchema()
{
configuration = new Configuration();
configuration.configure();
new SchemaExport(configuration)
.setHaltOnError(true)
.execute(false, true, false, false);
}
to create the DDL in an external file, use this call to execute.
要在外部文件中创建 DDL,请使用对execute.
new SchemaExport(configuration)
.setHaltOnError(true)
.setOutputFile(outputFile)
.setImportFile("")
.setDelimiter(";")
.setFormat(true)
.execute(false, false, false, true);
It's considered bad form to set "hibernate.hbm2ddl.auto" to "update", since automatically changing the production database may break it. For an explanation, see Hibernate hbm2ddl.auto possible values and what they do?.
将“hibernate.hbm2ddl.auto”设置为“update”被认为是不好的形式,因为自动更改生产数据库可能会破坏它。有关解释,请参阅Hibernate hbm2ddl.auto 可能的值及其作用?.
回答by Arjan Tijms
Adding to the list of James McMahon:
添加到 James McMahon 的列表中:
For OpenJPA:
对于 OpenJPA:
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
回答by DataNucleus
DataNucleushas its own SchemaTool capable of generating the schema for you, or generating the required DDL statements for you to adapt and apply yourself.
DataNucleus拥有自己的 SchemaTool,能够为您生成架构,或生成所需的 DDL 语句以供您自行调整和应用。
--Andy (DataNucleus)
--Andy(DataNucleus将)
回答by cybersoft
Antonio Goncalves says in his blogabout API to generate the schema.
In JPA 2.1 generateSchemamethod was introduced for this purpose.
Antonio Goncalves 在他的博客中谈到了生成模式的 API。为此,
在 JPA 2.1中引入了generateSchema方法。
Example from blog:
来自博客的示例:
public class Main {
public static void main(String[] args) {
Persistence.generateSchema("samplePU", null);
}
}

