在Maven测试中生成数据库架构(Hibernate) [英] Generate database schema (Hibernate) on maven test

查看:109
本文介绍了在Maven测试中生成数据库架构(Hibernate)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(一段时间没碰JPA)开始使用Hibernate 4.1,JPA2和Spring进行项目.这是一个具有共同生命周期的Maven项目.

I (after a while without touching JPA) started a project with Hibernate 4.1, JPA2 and Spring. It is a maven project with a common life cycle.

运行mvn测试时,得到未找到所需数据库对象的预期结果(连接按预期成功).但是,我所有的研究和实验都证明不足以制作出看起来很普遍的东西.

Upon running mvn test I get the expected result of not finding the required database objects (connection is successful as expected). All my researches and experiments, however, proved not enough to make something that seemed to be a common situation.

  • 我希望能够在执行单元测试时让maven在本地开发数据库中删除/创建模式;我以为hibernate3-maven-plugin(3.0版是理想的)应该可以解决这个问题,但是我没有设法使其工作.我不希望有任何自动插入数据的功能(为此,我可以使用DBUnit甚至更好的方法是让每个测试生成自己的测试数据,但这在这里不起作用),但是我确实希望在测试数据库上刷新该架构,反映我带注释的模型类的当前状态.我想这将绑定到process-test-resources阶段.

  • I expect to be able to have maven drop/create the schema the local development database while executing unit tests; I imagined that hibernate3-maven-plugin (version 3.0 ideally) should handle this but I didn't manage to have it working. I don't expect any automatic insertion of data (for this I could use DBUnit or even better have each test generate its own test data, but this plays no role here) but I do expect that the schema be refreshed on the test database, reflecting the current state of my annotated model classes. I suppose this would be bound to process-test-resources phase.

我希望使用(当前)架构定义生成一个(或一组)sql文件,但是我得到的最佳结果反映了此处描述的问题:(到目前为止还没有解决方案).

I expect to generate a (or set of) sql file with the (current) schema definition, but the best result I got reflects the issue described here: Maven + Spring + Hibernate: hibernate3-maven-plugin hbm2ddl fails for reason "Caused by: java.lang.NullPointerException" (no solution so far).

我是否错过了一些愚蠢的事情,或者这真的不是可能的吗? 如果有人可以为我提供

Am I missing something silly or is it really not possibile at this time? I would be very happy if someone could provide me any of

  • 有关如何实现此目标的正确文档

  • proper documentation of how this is supposed to be achieved

使用休眠4的工作示例

使用其他一些策略实现目标的实用准则.

any guidelines of practical ways of achieve my goals with some other strategy.

如果有任何相关性,则数据库为Postgres 9.1.

If it's of any relevance, database is Postgres 9.1.

谢谢.

推荐答案

再次回答我自己的问题.

Again an answer to my own question.

要获得结果,我希望我必须在进程类阶段将hibernate3-maven-plugin的hbm2ddl的执行与在进程-测试-资源阶段中的sql-maven-plugin的另一执行一起执行.第一个为Hibernate模型生成sql,第二个将它应用于数据库.

To achieve the result I wanted I had to chain in maven the execution of hibernate3-maven-plugin's hbm2ddl on process-classes phase with another execution of sql-maven-plugin on the process-test-resources phase. The first generates the sql for Hibernate's model and the second applies it to the database.

事后看来,这实际上是一个不错的解决方案.

With hindsight, seems actually a decent and clean solution.

如果最佳实践与我的解决方案有所不同,我仍然想了解最佳实践,以实现建立数据库以从Hibernate模型进行maven测试的目标.

I'm still interested in knowing the best practices, if they differ from my solution, to achieve the goal of setting up the database for testing with maven from a Hibernate model.

这篇关于在Maven测试中生成数据库架构(Hibernate)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆