hans
hans

Reputation: 15

How to create a database backup using H2 and JPA?

I am developing a JPA application which uses H2 as the database and I want to create a backup while the database is running.
Here is the relevant part of the persistence.xml:

  <persistence-unit name="examplePU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
 </persistence-unit>

This H2 Tutorial says that I have to use the sql statement "BACKUP TO 'backup.zip'", but I'm not quite sure how. My attempt so far:

entityManager.createQuery("BACKUP TO 'backup.zip'").executeUpdate();

But it results in this exception:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: BACKUP near line 1, column 1 [BACKUP TO 'backup.zip']

I was hoping, that I don't have to manually export the tables to JSON or CSV. While the export part works, the import is more problematic, since I have a lot of ManyToMany relations.

Upvotes: 0

Views: 419

Answers (1)

sare3th
sare3th

Reputation: 828

try entityManager.createNativeQuery(...) instead

Upvotes: 1

Related Questions