Hibernate 使用 hbm2ddl.auto=update 保留一些表,使用 hbm2ddl.auto=create 重新加载一些表

Posted

技术标签:

【中文标题】Hibernate 使用 hbm2ddl.auto=update 保留一些表,使用 hbm2ddl.auto=create 重新加载一些表【英文标题】:Hibernate preserve some tables with hbm2ddl.auto=update, reload some tables with hbm2ddl.auto=create 【发布时间】:2017-02-24 16:31:39 【问题描述】:

:)

顾名思义,我想问您是否可以在 hibernate 5.2.2 中对某些表使用 hbm2ddl.auto=update 而对其他表使用 hbm2ddl.auto=create。或者更好的是我可以在代码中明确定义现在我想创建新表而其他时候我只想更新?

HibernateConnector 的一个示例,我希望它如何工作:

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();


谢谢你,祝你有美好的一天:)

【问题讨论】:

【参考方案1】:

好的,所以我完全按照我在问题中提出的方式解决了这个问题。我使用简单的布尔标志来决定 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();


我不完全确定这是最好的解决方案,但它确实有效。

【讨论】:

以上是关于Hibernate 使用 hbm2ddl.auto=update 保留一些表,使用 hbm2ddl.auto=create 重新加载一些表的主要内容,如果未能解决你的问题,请参考以下文章

hibernate.hbm2ddl.auto =创建(不创建表)

使用 hibernate.hbm2ddl.auto create 找不到表

为啥即使使用 hibernate.hbm2ddl.auto" value="create",hibernate 也不会自动创建表?

Hibernate、MySQL 视图和 hibernate.hbm2ddl.auto = 验证

休眠 - hibernate.hbm2ddl.auto = 验证

hibernate.hbm2ddl.auto Hibernate 如何决定何时创建或更新 ddl?