1

I'm running a Spring MVC project and would like to have a form button that will reset all the data to a fresh Tomcat start. We're using Hibernate with an import.sql script to populate some dummy data at startup in dev environments. I'm looking for a solution whereby the instance will simply return to this state on click of a button (so essentially a Hibernate ddl-auto of create plus the import of the import.sql). This will of course not run in production.

I'd also be open to translating the contents of the import.sql to a programmatic (Java config-based) solution if that makes this easier.

There may be an answer to this question already on SO, but I haven't found it yet. Will close it if it's a duplicate.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80

2 Answers2

1

This SO question is very similar to what you need to do.

What you need to do is something like this on a click of the button.

    Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/?user=user&password=pass");
    Statement statement = (Statement) conn.createStatement();
    statement.executeUpdate("DROP DATABASE tcs;");
    statement.executeUpdate("CREATE DATABASE tcs charset=utf8 collate=utf8_persian_ci;");

Then you will need to call buildSessionFactory method which hibernate provides to recreate the tables.

I hope it helps.

Sadiq Ali
  • 1,272
  • 2
  • 15
  • 22
  • 1
    This seems like the closest solution so far, though I'm a bit surprised there isn't anything that's more automatic. Will give this question another few days. – riddle_me_this Sep 22 '17 at 13:29
  • Glad it worked for you, will look around a bit more to see if I can find something more straightforward. – Sadiq Ali Sep 22 '17 at 14:16
0

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]]
Nathan
  • 1,576
  • 8
  • 18
  • My exact implementation has the disadvantage, that you would have to keep the second sessionfactory open, otherwise it would drop your DB schema when you close it. But hopefully it gives you some helpful ideas. – Nathan Sep 22 '17 at 07:48