Generate database schema (Hibernate) on maven test

守給你的承諾、 提交于 2019-12-13 01:33:51

问题


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.

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.

  • 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.

  • 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

  • an working example using hibernate 4

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

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

Thanks in advance.


回答1:


One way of doing it is to use Arquillian. You can create a separate deployment package for each test or for a suite of tests, with it's own persistence.xml and datasources. Use the hbm2dll setting in the persistence.xml to either create-drop or update the schema:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

If you want to prepopulate the database, you can either add a import.sql to your deployment which will be executed by hibernate on application startup, or use the Arquillian Persistence extension.

Here is a complete arquillian test setup as example:

@RunWith(Arquillian.class)
public class MyTest {

    @Deployment
    public static Archive<?> createTestArchive() {

        return ShrinkWrap.create(WebArchive.class, "myTest.war")
                .addPackages(true, "com.example")
                .addAsResource("testSeeds/seed.sql", "import.sql")
                .addAsResource("in-mem-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource("in-mem-datasource.xml");
    }

One downside is that the in-container tests will be slower than simple unit tests.

I am not sure how well Arquillian is able to play nice with Spring, I have only used it for Java EE applications, so please tell if it is of any help.




回答2:


Again an answer to my own question.

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.

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.



来源:https://stackoverflow.com/questions/14886025/generate-database-schema-hibernate-on-maven-test

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