0

I'm trying to configure Spring and Hibernate without xml. Here's my SessionFactory bean. When I add annotated class to configuration - it works properly. I want to do it automatically, but adding a package to configuration doesnt helps for some reason, I get "Identificator is not mapped" error

    @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;
}
flgdev
  • 467
  • 1
  • 6
  • 17
  • possible duplicate of [Add Annotated Class in Hibernate by adding all classes in some package. JAVA](http://stackoverflow.com/questions/8122792/add-annotated-class-in-hibernate-by-adding-all-classes-in-some-package-java) – meskobalazs Feb 03 '15 at 09:28
  • @meskobalazs AnnotationConfiguration is deprecated – flgdev Feb 03 '15 at 09:32
  • Yes, I noticed it too late, however `addPackage` does not do what you expect to do, as it reads *package-level metadata* – meskobalazs Feb 03 '15 at 09:34

2 Answers2

0

The #addPackage() method reads the package-level metadata, and not the classes in the package. Unfortunately the Configuration class does not provide methods the achive what you want, you have to pass all classes to #addAnnotatedClass().

A possible (but not sure if recommendable) solution would be using another solution to find the required class descriptors, build a list from them, and then pass them in a loop to #addAnnotatedClass(). I am quite sure, that Spring has solutions for this.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
0
   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;
}
  • 2
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Dec 06 '17 at 15:59