I am developing a JPA application which uses H2 for the database. I want to create a backup (export as sql file) and import a backup from a webinterface.
The creation of the backup sql file and the upload of the sql file is working but I can't get the entityManager to properly execute the imported sql file.
I tried creating a file from the imported sql and then running it as follows:
public void applyBackup() {
em.flush();
Query query = em.createNativeQuery("DROP ALL OBJECTS");
query.executeUpdate();
query = em.createNativeQuery("RUNSCRIPT FROM 'backup.sql'");
query.executeUpdate();
}
The problem here is, that the file I created from the imported sql is located in the server folder, while the RUNSCRIPT query tries to run it from the project folder.
My second attempt is parse the sql into a string and then executing this as a sql command:
public void applyBackup(String sqlStr) {
em.flush();
Query query = em.createNativeQuery("DROP ALL OBJECTS");
query.executeUpdate();
query = em.createNativeQuery(sqlStr);
query.executeUpdate();
}
This doesn't throw an exeception during execution but when I try to get data out of the database, it says that table xy is not found.
To create the backup I use the following query:
Query query = em.createNativeQuery("SCRIPT TO 'backup.sql'");
As described here.