【发布时间】:2015-02-03 09:25:13
【问题描述】:
我正在尝试在没有 xml 的情况下配置 Spring 和 Hibernate。这是我的 SessionFactory bean。当我将带注释的类添加到配置中时-它可以正常工作。我想自动执行此操作,但由于某种原因将包添加到配置中没有帮助,我收到“标识符未映射”错误
@Bean
public SessionFactory sessionFactory(){
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.connection.driver_class",ds_driver);
hibernateProperties.put("hibernate.connection.url",ds_url);
hibernateProperties.put("hibernate.connection.username",ds_username);
hibernateProperties.put("hibernate.connection.password",ds_password);
hibernateProperties.put("hibernate.show_sql", false);
hibernateProperties.put("connection.pool_size", 1);
hibernateProperties.put("current_session_context_class", "thread");
hibernateProperties.put("hibernate.hbm2ddl.auto", "update");
org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration();
configuration.addPackage("app.entity"); // **doesnt work**
configuration.addAnnotatedClass(Identificator.class); // **works fine**
configuration.addProperties(hibernateProperties);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
【问题讨论】:
-
@meskobalazs AnnotationConfiguration 已弃用
-
是的,我注意到它为时已晚,但是
addPackage并没有按照您的预期做,因为它读取 包级元数据