使用hibernate注释导出模式

我正在使用hibernate注释,我想导出我的数据库架构。

类似于使用hbm xml文件的schemaexporttask。

实际上,原始的Hibernate Core SchemaExportTask只能处理Hibernate XML映射文件,而不能处理注释。 你需要的是Hibernate Tools附带的HibernateToolTask 。

这是一个改编自Java Persistence With Hibernate的用法示例:

         

也可以看看

  • Hibernate 3 Annotations&Ant

你可以 。 去做就对了

 AnnotationConfiguration configuration = new AnnotationConfiguration(); configuration .addAnnotatedClass(.class) .setProperty(Environment.USER, ) .setProperty(Environment.PASS, ) .setProperty(Environment.URL, ) .setProperty(Environment.DIALECT, ) .setProperty(Environment.DRIVER, ); SchemaExport schema = new SchemaExport(configuration); schema.setOutputFile("schema.sql"); schema.create(, ); 

如果有人有兴趣如何使用JPA + Spring从unit testing中执行此操作(您可以轻松地从Eclipse内部生成运行unit testing的sql):

ExportDatabaseSchema.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); } } 

您需要一个ExportDatabaseSchema-context.xml:

    

applicationContext-jpa.xml包含注释配置的entityManagerFactory bean。 诀窍是使用&注入它:“&entityManagerFactory”,取消引用弹簧生成的代理。

使用hibernate3-maven-plugin。 然后运行’mvn hibernate3:hbm2ddl’