Luka
Luka

Reputation: 63

Redis with jHipster - Default configuration hasn't been specified

im using jhipster version 7.6.0 and this is my CacheConfiguration class.

@Configuration
@EnableCaching
public class CacheConfiguration {

private GitProperties gitProperties;
private BuildProperties buildProperties;

@Bean
public javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration(JHipsterProperties jHipsterProperties) {
    MutableConfiguration<Object, Object> jcacheConfig = new MutableConfiguration<>();

    URI redisUri = URI.create(jHipsterProperties.getCache().getRedis().getServer()[0]);

    Config config = new Config();
    if (jHipsterProperties.getCache().getRedis().isCluster()) {
        ClusterServersConfig clusterServersConfig = config
            .useClusterServers()
            .setMasterConnectionPoolSize(jHipsterProperties.getCache().getRedis().getConnectionPoolSize())
            .setMasterConnectionMinimumIdleSize(jHipsterProperties.getCache().getRedis().getConnectionMinimumIdleSize())
            .setSubscriptionConnectionPoolSize(jHipsterProperties.getCache().getRedis().getSubscriptionConnectionPoolSize())
            .addNodeAddress(jHipsterProperties.getCache().getRedis().getServer());

        if (redisUri.getUserInfo() != null) {
            clusterServersConfig.setPassword(redisUri.getUserInfo().substring(redisUri.getUserInfo().indexOf(':') + 1));
        }
    } else {
        SingleServerConfig singleServerConfig = config
            .useSingleServer()
            .setConnectionPoolSize(jHipsterProperties.getCache().getRedis().getConnectionPoolSize())
            .setConnectionMinimumIdleSize(jHipsterProperties.getCache().getRedis().getConnectionMinimumIdleSize())
            .setSubscriptionConnectionPoolSize(jHipsterProperties.getCache().getRedis().getSubscriptionConnectionPoolSize())
            .setAddress(jHipsterProperties.getCache().getRedis().getServer()[0]);

        if (redisUri.getUserInfo() != null) {
            singleServerConfig.setPassword(redisUri.getUserInfo().substring(redisUri.getUserInfo().indexOf(':') + 1));
        }
    }
    jcacheConfig.setStatisticsEnabled(true);
    jcacheConfig.setExpiryPolicyFactory(
        CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, jHipsterProperties.getCache().getRedis().getExpiration()))
    );
    return RedissonConfiguration.fromInstance(Redisson.create(config), jcacheConfig);
}

Here is dependency for redisson :

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
</dependency>

This is my application-dev.yml:

application-dev.yml

Everything is generated by jHipster and after installation of redis version 3.2.100, im getting this exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalStateException: Default configuration hasn't been specified! at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) at com.eig.EigApp.main(EigApp.java:69) 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.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalStateException: Default configuration hasn't been specified! at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ... 20 common frames omitted Caused by: java.lang.IllegalStateException: Default configuration hasn't been specified! at org.redisson.jcache.JCacheManager.createCache(JCacheManager.java:118) at org.hibernate.cache.jcache.internal.JCacheRegionFactory.createCache(JCacheRegionFactory.java:112) at org.hibernate.cache.jcache.internal.JCacheRegionFactory.getOrCreateCache(JCacheRegionFactory.java:99) at org.hibernate.cache.jcache.internal.JCacheRegionFactory.createDomainDataStorageAccess(JCacheRegionFactory.java:83) at org.hibernate.cache.jcache.internal.JCacheRegionFactory.buildDomainDataRegion(JCacheRegionFactory.java:72) at org.hibernate.cache.internal.EnabledCaching.prime(EnabledCaching.java:113) at org.hibernate.metamodel.internal.MetamodelImpl.primeSecondLevelCacheRegions(MetamodelImpl.java:331) at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:160) at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:303) at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ... 24 common frames omitted

Project is compiling successfully by the way.

Thanks in advance for help!

Upvotes: 5

Views: 1499

Answers (1)

Luke
Luke

Reputation: 175

It's seem related to Redis works. You can checkout docs: https://www.baeldung.com/hibernate-second-level-cache move to "What Is a Second-Level Cache".

First, enable bean "hibernatePropertiesCustomizer" in CacheConfiguration.java:

import org.hibernate.cache.jcache.ConfigSettings;

@Bean
public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(CacheManager cm) {
    return hibernateProperties -> hibernateProperties.put(ConfigSettings.CACHE_MANAGER, cm);
}

Unfortunately, Redis in Jhipster don't initial cache for each entity model, so you must to initial cache for each model in domain package, like:

@Bean
    public JCacheManagerCustomizer cacheManagerCustomizer(javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration) {
        return cm -> {
            // jhipster-needle-redis-add-entry
            createCache(cm, com.project.model.YourModel1.class.getName(), jcacheConfiguration);
            createCache(cm, com.project.model.YourModel2.class.getName(), jcacheConfiguration);
        };
    }

Please add all your Model(Entity class) using @Cache annotation.
Good luck,

Upvotes: 0

Related Questions