我正在以编程方式配置我的 hibernate session 工厂:
private static SessionFactory buildSessionFactory() {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure();
configuration.setProperty("hibernate.connection.url", myUrl);
configuration.setProperty("hibernate.connection.username", myUser);
configuration.setProperty("hibernate.connection.password", myPass);
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
return configuration.buildSessionFactory(serviceRegistry);
}
但问题是,这些属性仅在使用 dao 的 hibernate 操作时加载。
protected void startOperation() {
session = HibernateUtil.getSessionFactory().openSession();
tx = session.beginTransaction();
}
因此,当我的应用程序启动时,hibernate.hbm2ddl.auto 似乎不起作用。我可以以某种方式强制 hibernate.hbm2ddl.auto 在我的程序或任何其他解决方案中启动吗?
建议或其他选择、想法?
最佳答案
需要设置hibernate.hbm2ddl.auto或者使用
configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop");
使用像 hibernate.properties 或 hibernate.cfg.xml 这样的配置文件是设置您的设置的首选方式。
关于java - 使用编程配置 hibernate ,启动 hibernate.hbm2ddl.auto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12636015/