gpt4 book ai didi

java - 使用编程配置 hibernate ,启动 hibernate.hbm2ddl.auto

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:20 25 4
gpt4 key购买 nike

我正在以编程方式配置我的 hibernate session 工厂:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html#configuration-programmatic

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.propertieshibernate.cfg.xml 这样的配置文件是设置您的设置的首选方式。

关于java - 使用编程配置 hibernate ,启动 hibernate.hbm2ddl.auto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12636015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com