java 带有休眠注释的模式导出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3393092/
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
Schema export with hibernate annotations
提问by Anthony
I'm using hibernate annotations and i want to export my database schema.
我正在使用休眠注释并且我想导出我的数据库架构。
Similar to the schemaexporttask with hbm xml files.
类似于带有 hbm xml 文件的 schemaexporttask。
采纳答案by Pascal Thivent
Indeed, the original Hibernate Core SchemaExportTaskcan only handle Hibernate XML mapping files, not annotations. What you need is the HibernateToolTaskthat comes with Hibernate Tools.
确实,原来的 Hibernate CoreSchemaExportTask只能处理 Hibernate XML 映射文件,不能处理注解。您需要的是Hibernate ToolsHibernateToolTask附带的。
Here is an Usage example adapted from Java Persistence With Hibernate:
这是一个改编自 Java Persistence With Hibernate 的用法示例:
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="project.classpath"/>
<target name="schemaexport" depends="compile, copymetafiles"
description="Exports a generated schema to DB and file">
<hibernatetool destdir="${basedir}">
<classpath path="${build.dir}"/>
<configuration
configurationfile="${build.dir}/hibernate.cfg.xml"/>
<hbm2ddl
drop="true"
create="true"
export="true"
outputfilename="helloworld-ddl.sql"
delimiter=";"
format="true"/>
</hibernatetool>
</target>
See also
也可以看看
回答by Arthur Ronald
You can. Just do it
你可以。去做就对了
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration
.addAnnotatedClass(<TYPE_YOUR_CLASS>.class)
.setProperty(Environment.USER, <TYPE_YOUR_USER>)
.setProperty(Environment.PASS, <TYPE_YOUR_PASSWORD>)
.setProperty(Environment.URL, <TYPE_YOUR_URL>)
.setProperty(Environment.DIALECT, <TYPE_YOUR_DIALECT>)
.setProperty(Environment.DRIVER, <TYPE_YOUR_DRIVER>);
SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("schema.sql");
schema.create(<DO_YOU_WANT_TO_PRINT_TO_THE_CONSOLE>, <DO_YOU_WANT_TO_EXPORT_THE_SCRIPT_TO_THE_DATABASE>);
回答by Norbert Madarász
In case someone is interested how to do this with JPA+Spring from a unit test (you can generate the sql running the unit test from inside Eclipse like a breeze):
如果有人对如何从单元测试中使用 JPA+Spring 执行此操作感兴趣(您可以轻松地从 Eclipse 内部生成运行单元测试的 sql):
ExportDatabaseSchema.java:
导出数据库架构.java:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration(defaultRollback=true)
public class ExportDatabaseSchema {
@Resource(name="&entityManagerFactory")
private LocalContainerEntityManagerFactoryBean entityManagerFactory;
@Test
public void exportDatabaseSchema(){
PersistenceUnitInfo persistenceUnitInfo = entityManagerFactory.getPersistenceUnitInfo();
Map jpaPropertyMap = entityManagerFactory.getJpaPropertyMap();
Configuration configuration = new Ejb3Configuration().configure( persistenceUnitInfo, jpaPropertyMap ).getHibernateConfiguration();
SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("resources/sql/schema.sql");
schema.create(false, false);
}
}
You need an ExportDatabaseSchema-context.xml:
您需要一个 ExportDatabaseSchema-context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="classpath:applicationContext-jpa.xml" />
</beans>
The applicationContext-jpa.xml contains the annotation configured entityManagerFactory bean. The trick is to inject it with & like this: "&entityManagerFactory", to dereference the spring generated proxy.
applicationContext-jpa.xml 包含注解配置的 entityManagerFactory bean。诀窍是像这样用 & 注入它:“&entityManagerFactory”,取消引用 spring 生成的代理。
回答by YNChumak
Use hibernate3-maven-plugin. Then run 'mvn hibernate3:hbm2ddl'
使用 hibernate3-maven-plugin。然后运行'mvn hibernate3:hbm2ddl'

