Java 当datasource是由配置bean定义的而不是xml时,如何创建SessionFactorybean

Java 当datasource是由配置bean定义的而不是xml时,如何创建SessionFactorybean,java,hibernate,Java,Hibernate,冬眠5.4 方言是已知的,我需要实现以下方法: @Bean public org.hibernate.SessionFactory sessionFactory(DataSource dataSource) { // hibernate.dialect = "org.hibernate.dialect.PostgreSQL95Dialect" // ??? } 也许通过配置创建一个会对您有用,如下所述: Configuration cfg=新配置() .s

冬眠5.4

方言是已知的,我需要实现以下方法:

@Bean
public org.hibernate.SessionFactory sessionFactory(DataSource dataSource) {
    // hibernate.dialect = "org.hibernate.dialect.PostgreSQL95Dialect"
    // ???
}

也许通过配置创建一个会对您有用,如下所述:

Configuration cfg=新配置()

.setProperty(“hibernate.connection.username”、“myuser”); .setProperty(“hibernate.connection.password”、“mypassword”) .setProperty(“hibernate.connection.url”,“jdbc:mysql://localhost:3306/hibernate_example))

SessionFactory SessionFactory=cfg.buildSessionFactory()

或者选择使用Spring LocalSessionFactory类,如下所示:

lsfb = new LocalSessionFactoryBean()  [from hib5-package]
lsfb.setDataSource( yourDS );
return lsfb.getObject();
设置数据源这可能有助于:

但是,如果您使用一个定制的数据源提供程序,如ApacheDBCP或BoneCP,并且不想使用依赖项注入框架,如Spring,那么您可以在创建SessionFactory之前将其注入StandardServiceRegistryBuilder