开发者

Programmatically create SessionFactory in Spring

开发者 https://www.devze.com 2023-03-16 16:28 出处:网络
Suppose I programmatically create a AnnotationSessionFactoryBean and set the various properties correctly. How can I then extract the Hibernate SessionFactory, since all methods that pertain to creati

Suppose I programmatically create a AnnotationSessionFactoryBean and set the various properties correctly. How can I then extract the Hibernate SessionFactory, since all methods that pertain to creating the SessionFactory are protected?

AnnotationSess开发者_开发知识库ionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
SessionFactory sessionFactory = sessionFactoryBean.newSessionFactory(); // Protected!!


Use getObject(), after calling afterPropertiesSet():

sessionFactoryBean.afterPropertiesSet();
SessionFactory sessionFactory = sessionFactoryBean.getObject();

(AnnotationSessionFactoryBean implements FactoryBean<SessionFactory>)

Be careful, though: by doing this, it becomes your responsibility to make sure the SessionFactory is closed when you're finished with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消