I am configuration my hibernate sessionfactory programmatically:
我正在以编程方式配置我的hibernate sessionfactory:
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);
}
But problem, is that these properties are loaded only, when using hibernate operation from dao.
但是问题是,当从dao使用hibernate操作时,这些属性只被加载。
protected void startOperation() {
session = HibernateUtil.getSessionFactory().openSession();
tx = session.beginTransaction();
}
Therefore, when my application boots up, then hibernate.hbm2ddl.auto doesn't seem to work. Can I somehow force hibernate.hbm2ddl.auto to start in my program or any other solution?
因此,当我的应用程序启动时,然后hibernate.hbm2ddl。汽车似乎不能工作。我可以强制hibernate.hbm2ddl吗?自动启动我的程序或其他解决方案?
Suggetions or other options, thoughts?
建议还是其他选择,想法?
2 个解决方案
#1
3
You need to set hibernate.hbm2ddl.auto or used
您需要设置hibernate.hbm2ddl。auto or used
configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop");
Using configuration file like hibernate.properties or hibernate.cfg.xml is more preferred way to set your setting.
使用配置文件如hibernate。属性或hibernate.cfg。xml是设置设置设置的首选方式。
#2
0
Yes. new Configuration()
should load all the properties from hibernate.cfg.xml
.
是的。新配置()应该加载hibernate.cfg.xml的所有属性。
It seems that your SessionFactory
is configured to be lazy initialized which only be built when HibernateUtil.getSessionFactory()
is called.
似乎您的SessionFactory被配置为惰性初始化,只在调用hibernate . getsessionfactory()时构建。
If it is a console program , simple call SessionFactory.buildSessionFactory()
in the main method
如果是控制台程序,可以在主方法中简单地调用SessionFactory.buildSessionFactory()
If it is a web application , you can use ServletContextListener.contextInitialized(ServletContextEvent sce) or Spring to force the SessionFactory.buildSessionFactory()
to be executed during the server starts.
如果是web应用程序,则可以使用ServletContextListener。contextInitialized(servlettevent sce)或Spring在服务器启动期间强制执行SessionFactory.buildSessionFactory()。
#1
3
You need to set hibernate.hbm2ddl.auto or used
您需要设置hibernate.hbm2ddl。auto or used
configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop");
Using configuration file like hibernate.properties or hibernate.cfg.xml is more preferred way to set your setting.
使用配置文件如hibernate。属性或hibernate.cfg。xml是设置设置设置的首选方式。
#2
0
Yes. new Configuration()
should load all the properties from hibernate.cfg.xml
.
是的。新配置()应该加载hibernate.cfg.xml的所有属性。
It seems that your SessionFactory
is configured to be lazy initialized which only be built when HibernateUtil.getSessionFactory()
is called.
似乎您的SessionFactory被配置为惰性初始化,只在调用hibernate . getsessionfactory()时构建。
If it is a console program , simple call SessionFactory.buildSessionFactory()
in the main method
如果是控制台程序,可以在主方法中简单地调用SessionFactory.buildSessionFactory()
If it is a web application , you can use ServletContextListener.contextInitialized(ServletContextEvent sce) or Spring to force the SessionFactory.buildSessionFactory()
to be executed during the server starts.
如果是web应用程序,则可以使用ServletContextListener。contextInitialized(servlettevent sce)或Spring在服务器启动期间强制执行SessionFactory.buildSessionFactory()。