迁移到Hibernate 4后,在集成测试中进行配置设置

时间:2013-02-17 22:42:20

标签: hibernate integration-testing hibernate-4.x

我有配置类

public class HibernateConfiguration {

    private HibernateConfiguration() {}

    public Configuration configuration(Class clazz) {
        Configuration c = new Configuration();
        c.setProperty(Environment.DRIVER, "org.postgresql.Driver");
        c.setProperty(Environment.URL, "jdbc:postgresql://localhost/brush_and_pixel");
        c.setProperty(Environment.USER, "postgres");
        c.setProperty(Environment.PASS, "solaris");
        c.setProperty(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
        c.setProperty(Environment.AUTOCOMMIT, "true"); //"true");
            c.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
            c.addClass(clazz);

        return c;
    }

    public static HibernateConfiguration getHibernateConfiguration() {
        return new HibernateConfiguration();
    }

}

我正在测试中访问它:

public void setUp() {
    /**/
    Configuration c = HibernateConfiguration.getHibernateConfiguration()
        .configuration(SettingsGroup.class).addClass(Setting.class);

    SessionFactory sessionFactory =  c.buildSessionFactory();

    settingsGroupDao = new SettingGroupDao();
    settingsGroupDao.setSessionFactory(sessionFactory);
}

由于sessionFactory方法已被弃用,建议获取buildSessionFactory()的方式是什么?

1 个答案:

答案 0 :(得分:0)

来自hibernate docs

示例2.4。获取org.hibernate.SessionFactory

protected void setUp() throws Exception {
    // A SessionFactory is set up once for an application
    sessionFactory = new Configuration()
            .configure() // configures settings from hibernate.cfg.xml
            .buildSessionFactory();
}