javamysqlhibernatehbm2ddl

Hibernate preserve some tables with hbm2ddl.auto=update, reload some tables with hbm2ddl.auto=create


:)

As the name suggests I'd like to ask you if in hibernate 5.2.2, it is possible to use hbm2ddl.auto=update for some tables while using hbm2ddl.auto=create for others. Or better can I specifically define in code that now I want to create new tables and some other time I only want to update?

An example of HibernateConnector how I would like it to function:

public class HibernateConnector {

private static HibernateConnector me;
private Configuration cfg;
private SessionFactory sessionFactory;


private HibernateConnector(boolean db) throws HibernateException {
    if (db) {
        cfg = new Configuration();
        cfg.addAnnotatedClass(A.class);
        cfg.addAnnotatedClass(B.class);

        cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        cfg.setProperty("hibernate.connection.url", "jdbc:mysql:XXX");
        cfg.setProperty("hibernate.connection.username", "XXX");
        cfg.setProperty("hibernate.connection.password", "XYZ");
        cfg.setProperty("hibernate.show_sql", "true");
        cfg.setProperty("hibernate.hbm2ddl.auto", "create");
        sessionFactory = cfg.buildSessionFactory();
    } else {
        cfg = new Configuration();
        cfg.addAnnotatedClass(A.class);
        cfg.addAnnotatedClass(B.class);
        cfg.addAnnotatedClass(C.class);
        cfg.addAnnotatedClass(D.class);

        cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        cfg.setProperty("hibernate.connection.url", "jdbc:mysql://XXX");
        cfg.setProperty("hibernate.connection.username", "XYZ");
        cfg.setProperty("hibernate.connection.password", "XXX");
        cfg.setProperty("hibernate.show_sql", "true");
        cfg.setProperty("hibernate.hbm2ddl.auto", "update");
        sessionFactory = cfg.buildSessionFactory();
    }
}

public static synchronized HibernateConnector getInstance() throws HibernateException {
    if (me == null) {
        me = new HibernateConnector(false);
    }
    return me;
}

public static synchronized HibernateConnector getDBInstance() throws HibernateException {
    if (me == null) {
        me = new HibernateConnector(true);
    }
    return me;
}

public Session getSession() throws HibernateException {
    Session session = sessionFactory.openSession();
    if (!session.isConnected()) {
        this.reconnect();
    }
    return session;
}

private void reconnect() throws HibernateException {
    this.sessionFactory = cfg.buildSessionFactory();
}
}

Thank you and have a nice day :)


Solution

  • Ok so I solved this exactly the way I have it in my question. I use simple boolean flag to decide between two separate configurations for HibernateConnector

    private HibernateConnector(boolean db) throws HibernateException {
    if (db) {
        cfg = new Configuration();
        cfg.addAnnotatedClass(A.class);
        cfg.addAnnotatedClass(B.class);
    
        cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        cfg.setProperty("hibernate.connection.url", "jdbc:mysql:XXX");
        cfg.setProperty("hibernate.connection.username", "XXX");
        cfg.setProperty("hibernate.connection.password", "XYZ");
        cfg.setProperty("hibernate.show_sql", "true");
        cfg.setProperty("hibernate.hbm2ddl.auto", "create");
        sessionFactory = cfg.buildSessionFactory();
    } else {
        cfg = new Configuration();
        cfg.addAnnotatedClass(A.class);
        cfg.addAnnotatedClass(B.class);
        cfg.addAnnotatedClass(C.class);
        cfg.addAnnotatedClass(D.class);
    
        cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        cfg.setProperty("hibernate.connection.url", "jdbc:mysql://XXX");
        cfg.setProperty("hibernate.connection.username", "XYZ");
        cfg.setProperty("hibernate.connection.password", "XXX");
        cfg.setProperty("hibernate.show_sql", "true");
        cfg.setProperty("hibernate.hbm2ddl.auto", "update");
        sessionFactory = cfg.buildSessionFactory();
    }
    }
    

    I am not completely sure this is the best solution, but it works.