Why not just create a new SessionFactory configured with "create-drop" using the same jdbcurl and then when you are done, you load and execute the contents of your import.sql script?
@Test
public void test() {
HsqlSessionFactoryCreator creator = HsqlSessionFactoryCreator.getInstance();
try (SessionFactory sessionFactory1 = creator.create()) {
try (Session session = sessionFactory1.openSession()) {
Transaction transaction = session.beginTransaction();
try {
createEntity(...);
transaction.commit();
} catch (Throwable throwable) {
transaction.rollback();
}
}
try (Session session = sessionFactory1.openSession()) {
System.err.println("SessionFactory1 " + session.createCriteria(MyEntity.class).list());
}
try (Session session = sessionFactory1.openSession()) {
Transaction transaction = session.beginTransaction();
try {
createEntity(...);
transaction.commit();
} catch (Throwable throwable) {
transaction.rollback();
}
}
try (Session session = sessionFactory1.openSession()) {
System.err.println("SessionFactory1 " + session.createCriteria(MyEntity.class).list());
}
try (SessionFactory sessionFactory2 = creator.create()) {
try (Session session = sessionFactory1.openSession()) {
System.err.println("SessionFactory1 after creating SessionFactory2 " + session.createCriteria(MyEntity.class).list());
}
try (Session session = sessionFactory2.openSession()) {
System.err.println("SessionFactory2 " + session.createCriteria(MyEntity.class).list());
}
try (Session session = sessionFactory1.openSession()) {
Transaction transaction = session.beginTransaction();
try {
createEntity(...);
transaction.commit();
} catch (Throwable throwable) {
transaction.rollback();
}
}
try (Session session = sessionFactory1.openSession()) {
System.err.println("SessionFactory1 " + session.createCriteria(MyEntity.class).list());
}
try (Session session = sessionFactory2.openSession()) {
System.err.println("SessionFactory2 " + session.createCriteria(MyEntity.class).list());
}
}
}
}
When I run the test (having created a SessionFactory with knowledge about an entity) I get the following output
SessionFactory1 [MyEntity[ID 9c81b1fa-04f5-4572-a945-e16d13ffc187]]
SessionFactory1 [MyEntity[ID 550e7c5d-6c8c-40ea-8f4b-cfdbf2d92075], MyEntity[ID 9c81b1fa-04f5-4572-a945-e16d13ffc187]]
SessionFactory1 after creating SessionFactory2 []
SessionFactory2 []
SessionFactory1 [MyEntity[ID 3748127d-6872-4d9c-84fb-1d83b85c9fe0]]
SessionFactory2 [MyEntity[ID 3748127d-6872-4d9c-84fb-1d83b85c9fe0]]