Hello,
In terms of migration to 5.2 I have the following no-XML configuration (except of mappings) stuff:
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:core.properties" })
@ComponentScan(value = "com.test")
public class HibernateConfiguration {
private static Logger logger = Logger.getLogger(HibernateConfiguration.class);
private final static String HIBERNATE_DIALECT = "org.hibernate.dialect.PostgreSQL95Dialect";
@Autowired private Environment env;
@Autowired private ResourceLoader resourceLoader;
@Bean
public SessionFactory sessionFactory() throws IOException {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(dataSource());
localSessionFactoryBean.setPackagesToScan(new String[] { "com.test.core.domain.*" });
localSessionFactoryBean.setMappingLocations(loadResources());
localSessionFactoryBean.setHibernateProperties(hibernateProperties());
localSessionFactoryBean.afterPropertiesSet();
return localSessionFactoryBean.getObject();
}
private Resource[] loadResources() {
Resource[] resources = null;
try {
resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
.getResources("classpath:/hibernate/**/*.hbm.xml");
} catch (IOException e) {
e.printStackTrace();
}
return resources;
}
@Bean
public DataSource dataSource() {
ComboPooledDataSource сomboPooledDataSource = new ComboPooledDataSource();
try {
сomboPooledDataSource.setDriverClass(Preconditions.checkNotNull(env.getProperty("jdbc.driver-class-name")));
} catch( PropertyVetoException pve ){
logger.error("Cannot load datasource driver (" + env.getProperty("jdbc.driver-class-name") +"): " + pve.getMessage());
return null;
}
сomboPooledDataSource.setJdbcUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
сomboPooledDataSource.setUser(Preconditions.checkNotNull(env.getProperty("jdbc.username")));
сomboPooledDataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.password")));
сomboPooledDataSource.setMinPoolSize(20);
сomboPooledDataSource.setMaxPoolSize(50);
сomboPooledDataSource.setCheckoutTimeout(15);
сomboPooledDataSource.setMaxStatements(0);
сomboPooledDataSource.setIdleConnectionTestPeriod(30);
return сomboPooledDataSource;
}
@Bean
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) throws Exception{
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
private static Properties hibernateProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", HIBERNATE_DIALECT);
properties.setProperty("hibernate.bytecode.use_reflection_optimizer", "true");
properties.setProperty("hibernate.show_sql", "false");
properties.setProperty("hibernate.hbm2ddl.auto", "validate");
properties.setProperty("hibernate.default_batch_fetch_size", "1000");
properties.setProperty("hibernate.max_fetch_depth", "2");
properties.setProperty("hibernate.generate_statistics", "false");
properties.setProperty("hibernate.default_schema", "EDRIVE");
properties.setProperty("hibernate.connection.CharSet", "utf8");
properties.setProperty("hibernate.connection.characterEncoding", "utf8");
properties.setProperty("hibernate.connection.useUnicode", "true");
properties.setProperty("hibernate.connection.release_mode", "after_transaction");
properties.setProperty("hibernate.jdbc.batch_size", "50");
properties.setProperty("hibernate.jdbc.fetch_size", "500");
properties.setProperty("hibernate.jdbc.use_scrollable_resultset", "false");
properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
properties.setProperty("hibernate.cache.use_query_cache", "true");
properties.setProperty("hibernate.cache.use_second_level_cache", "true");
properties.setProperty("hibernate.cache.use_structured_entries", "false");
properties.setProperty("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
return properties;
}
}
The NullPointerException raises when I run my test app:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579)
... 75 more
Caused by: java.lang.NullPointerException
at org.hibernate.metamodel.internal.AttributeFactory.getMetaModelType(AttributeFactory.java:202)
at org.hibernate.metamodel.internal.AttributeFactory.buildPluralAttribute(AttributeFactory.java:176)
at org.hibernate.metamodel.internal.AttributeFactory.buildAttribute(AttributeFactory.java:82)
at org.hibernate.metamodel.internal.MetadataContext.wrapUp(MetadataContext.java:213)
at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:220)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:300)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:460)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:535)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:519)
at com.test.core.config.HibernateConfiguration.sessionFactory(HibernateConfiguration.java:46)
at com.test.core.config.HibernateConfiguration$$EnhancerBySpringCGLIB$$8f9edb09.CGLIB$sessionFactory$2(<generated>)
at com.test.core.config.HibernateConfiguration$$EnhancerBySpringCGLIB$$8f9edb09$$FastClassBySpringCGLIB$$4b721c9d.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
at com.test.core.config.HibernateConfiguration$$EnhancerBySpringCGLIB$$8f9edb09.sessionFactory(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 76 more
Where my code is wrong, what should I add (I suppose this is related to Metadata?) to get this working like it was working in the 5.1?