【问题标题】:Hibernate package scan in JavaConfigJavaConfig 中的 Hibernate 包扫描
【发布时间】: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;
}

【问题讨论】:

标签: java spring hibernate


【解决方案1】:

#addPackage() 方法读取包级元数据,而不是包中的类。不幸的是,Configuration 类没有提供你想要的方法,你必须将所有类传递给#addAnnotatedClass()

一种可能(但不确定是否值得推荐)的解决方案是使用另一种解决方案来查找所需的类描述符,从中构建一个列表,然后将它们循环传递给#addAnnotatedClass()。我很确定,Spring 对此有解决方案。

【讨论】:

    【解决方案2】:
       public SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            Configuration configuration = new Configuration()
                    .configure(getMappedValue("Universal", "qb_hibernate"))
                    .setProperty("hibernate.connection.autocommit", "true")
                    .setProperty("connection.pool_size", "20000")
                    .setProperty("hibernate.dialect", getMappedValue("Universal", "dialect"))
                    .setProperty("hibernate.connection.driver_class", getMappedValue("Universal", "driver_class"))
                    .setProperty("hibernate.connection.url", getMappedValue("Universal", "url"))
                    .setProperty("hibernate.connection.username", getMappedValue("Universal", "userName"))
                    .setProperty("hibernate.connection.password", getMappedValue("Universal", "password"))
                    .setProperty("hibernate.show_sql", "false")
                    .setProperty("hibernate.current_session_context_class", "thread")
                    .setProperty("hibernate.query.factory_class", "org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory");
            ServiceRegistry serviceRegistry
                    = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();
            LocalSessionFactoryBuilder builder
                    = new LocalSessionFactoryBuilder(dataSource());
            builder.scanPackages("zw.co.techno.xxxxx.model").buildSettings(serviceRegistry);
            // builds a session factory from the service registry
            // sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            sessionFactory = builder.buildSessionFactory();
        }
        return sessionFactory;
    }
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    猜你喜欢
    • 1970-01-01
    • 2016-05-16
    • 2016-10-21
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    相关资源
    最近更新 更多