如何使用 H2 和 JPA 创建数据库备份?
How to create a database backup using H2 and JPA?
我正在开发一个使用 H2 作为数据库的 JPA 应用程序
我想在数据库为 运行.
时创建备份
这是 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>
这个 H2 Tutorial 说我必须使用 sql 语句 "BACKUP TO 'backup.zip'",但我不太确定如何使用。
到目前为止我的尝试:
entityManager.createQuery("BACKUP TO 'backup.zip'").executeUpdate();
但它导致了这个异常:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: BACKUP near line 1, column 1 [BACKUP TO 'backup.zip']
我希望我不必手动将表格导出到 JSON 或 CSV。虽然导出部分有效,但导入问题更多,因为我有很多 ManyToMany 关系。
改用entityManager.createNativeQuery(...)
我正在开发一个使用 H2 作为数据库的 JPA 应用程序
我想在数据库为 运行.
时创建备份
这是 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>
这个 H2 Tutorial 说我必须使用 sql 语句 "BACKUP TO 'backup.zip'",但我不太确定如何使用。 到目前为止我的尝试:
entityManager.createQuery("BACKUP TO 'backup.zip'").executeUpdate();
但它导致了这个异常:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: BACKUP near line 1, column 1 [BACKUP TO 'backup.zip']
我希望我不必手动将表格导出到 JSON 或 CSV。虽然导出部分有效,但导入问题更多,因为我有很多 ManyToMany 关系。
改用entityManager.createNativeQuery(...)