如果要创建SessionFactory(例如单元测试),请使用Hibernate 4.3.5,您必须提供两次属性:
示例如下:
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上使用Coda。
答案 0 :(得分:2)
尝试以下代码,它使用配置中的属性。
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);