Hibernate 4.3,当构建SessionFactory时为什么我们必须提供两次属性? [英] Hibernate 4.3, when building SessionFactory why do we have to supply the properties twice?

查看:191
本文介绍了Hibernate 4.3,当构建SessionFactory时为什么我们必须提供两次属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Hibernate 4.3.5当你想创建一个SessionFactory(例如单元测试),你必须提供属性两次:

Using the Hibernate 4.3.5 when you want to create a SessionFactory (e.g. unit testing) you will have to supply the properties twice:


  1. 一次配置

  2. 第二次将设置应用于服务注册表构建器

示例如下:

Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:test");
properties.put("hibernate.connection.username", "sa");
properties.put("hibernate.connection.password", "");
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.show_sql", "true");

SessionFactory sessionFactory = new Configuration()
    .addProperties(properties)
    .addAnnotatedClass(SecurityId.class)
    .buildSessionFactory(
            new StandardServiceRegistryBuilder()
                    .applySettings(properties)
                    .build()
);  

如果我评论:

//.addProperties(properties)

然后hibernate.hbm2ddl。 auto属性不彻底:

Then the "hibernate.hbm2ddl.auto" property doesn't get thorough:

Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: SECURITYID

如果我评论:

//.applySettings(properties)



<没有设置'hibernate.dialect'时,对DialectResolutionInfo的访问不能为空


I the get:

org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 

Coda可在 GitHub

推荐答案

尝试以下代码,它使用配置中的属性。

Try the following code, it uses properties from configuration.

Configuration configuration = new Configuration();
configuration.addAnnotatedClass(SecurityId.class);
configuration.addProperties(properties);
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

这篇关于Hibernate 4.3,当构建SessionFactory时为什么我们必须提供两次属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆