java Hibernate 4.3,在构建SessionFactory时,为什么必须提供两次属性?
使用Hibernate 4.3.5创建SessionFactory(例如单元测试)时,必须提供两次属性:
- 一次用于配置
- 第二次,将设置应用于service registry builder时
示例如下所示:
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”属性就不完整了:
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: SECURITYID
如果我评论:
//.applySettings(properties)
我得到:
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
尾码可在GitHub上找到
# 1 楼答案
尝试以下代码,它使用配置中的属性