Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute sql-script file using hibernate?

I gonna write several intergration tests which will test interatcion with db. For each test I need to have a certain snapshot of db. Each db snapshot saved in .sql file. What I want is to execute certain script file in certain test method, like this:

@Test
public void test_stuff(){
   executeScript(finame.sql);

   ... testing logic ...

   clean_database();
}

Does hibernate has some means to do this?

like image 670
Vladimir Avatar asked Oct 23 '25 03:10

Vladimir


2 Answers

  • You can automatically execute SQL script at startup of hibernate: write your SQL commands in a file called import.sql and put it in the root of the CLASSPATH.

  • You don't need to clean your database for your test, just make your test as transactional with rollback at the end of each test. Hence, you are sure your database is not contaminated by your tests. For instance, using Spring:

    @Transactional
    @TransactionConfiguration
    public class MyTest {
    ...
    }
    

If you don't use Spring, try a test framework with default-rollback transaction support.

like image 96
Kartoch Avatar answered Oct 25 '25 19:10

Kartoch


The topic of deprecated Session.connection() method is dicussed here

like image 29
Rots Avatar answered Oct 25 '25 19:10

Rots



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!