Hibernate JPA to DDL command line tools

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

There are Hibernate tools for mapping files to ddl generation; ddl to mapping files and so on, but I can't find any command line tools for simple DDL generation from JPA annotated classes.

Does anyone know an easy way to do this? (Not using Ant or Maven workarounds)

回答1:

I'm not sure, whether this is considered a workaround, because you already referred to it in your question. You can use Hibernate Tools to generate DDL from JPA annotated classes. You just need hibernate tools and its dependencies on the classpath and should be fine with something like the following:

<target name="schemaexport" description="Export schema to DDL file"     depends="compile-jpa"> <!-- compile model classes before running hibernatetool -->    <!-- task definition; project.class.path contains all necessary libs -->   <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"       classpathref="project.class.path" />    <hibernatetool destdir="export/db"> <!-- check that directory exists -->     <jpaconfiguration persistenceunit="myPersistenceUnitName" />     <classpath>       <!--           compiled model classes and other configuration files don't forget           to put the parent directory of META-INF/persistence.xml here       -->     </classpath>     <hbm2ddl outputfilename="schemaexport.sql" format="true"         export="false" drop="true" />   </hibernatetool> </target> 

On the other hand, if you are using Eclipse with Webtools and have configured the project settings correctly, you can just right click and select Generate DDL from the context menu. More information about that on the Eclipse Dali website.



回答2:

Here's an explaination of how to use the hibernate SchemaExport class to do what you want. Similar to the anttask method mentioned before, but not everyone uses ant. You can execute this example code right from the commandline.

http://jandrewthompson.blogspot.com/2009/10/how-to-generate-ddl-scripts-from.html

Hope this helps.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!