使用 Hibernate 4.3.5 当您想要创建 SessionFactory (例如单元测试)时,您必须提供属性两次:
- 一次配置
- 第二次,将设置应用到服务注册表构建器时
该示例如下所示:
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 https://github.com/vladmihalcea/hibernate-master-class/blob/master/src/test/java/com/vladmihalcea/DriverConnectionProviderTest.java.