简体   繁体   中英

Session factory in hibernate

会话工厂是休眠中的重量级对象,因此经常调用重量级对象是错误的,因此如果有人有实例,那么如何在整个项目中使用会话工厂的单个对象,请分享

In a Spring context, expose the sessionFactory as a bean.

@Bean
public SessionFactory sessionFactory() {
    final Properties properties = new Properties();

    final LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource()).scanPackages("yourPackageNameHere");

    builder.addProperties(properties);

    return builder.buildSessionFactory();
}

The use the SessionFactory bean in another class

@Autowired private SessionFactory sessionFactory;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM