controlling hibernate sql script run

660 views Asked by At

I am using Spring 4.1.6, and I have my service working fine with Hibernate. In the root of the project I've got my schema.sql which is being run every time I run the server. The problem is that first time I ran the server, I put some data in db, and when I restarted it, the script was executed again and I lost all that data that I loaded before restart.

So, I think that I have two options two solve this problem:

  1. Edit sql script to execute all queries just in case they do not exist (which would be more laborious since I have to edit the script every time I export my db)
  2. Tell hibernate, by some way, to execute sql script just in some cases. That would be great if there existed some config that executes the script just in case the data base doesn't exist.

Do you know if this is even possible? Thanks in advance.

1

There are 1 answers

1
StuPointerException On BEST ANSWER

It sounds like this is the perfect use-case for a tool called Liquibase. This is basically a version control tool for your database which allows you to define changes to your schema and/or data and ensures that these changes are only applied once.

It's incredibly useful if multiple people are changing the same database schema and ensures that your database is always valid for the version of the code that you have checked out/released etc.