Hibernate 使用 hbm2ddl.auto=update 保留一些表,使用 hbm2ddl.auto=create 重新加载一些表
Hibernate preserve some tables with hbm2ddl.auto=update, reload some tables with hbm2ddl.auto=create
:)
顾名思义,我想问一下,在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();
}
}
谢谢你,祝你有愉快的一天:)
好的,所以我完全按照问题中的方式解决了这个问题。我使用简单的布尔标志来决定 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 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();
}
}
谢谢你,祝你有愉快的一天:)
好的,所以我完全按照问题中的方式解决了这个问题。我使用简单的布尔标志来决定 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();
}
}
我不完全确定这是最好的解决方案,但它确实有效。