如何解决“所有Hibernate缓存都应先创建。请更新CacheConfiguration.java以添加com.nissan.rca.domain.OCIOMacroTypeOfService'

时间:2019-02-07 15:50:24

标签: java hibernate spring-boot caching jhipster

我对JHipster及其使用的技术还很陌生。我最近加入的一个项目是使用JHipster进行的,它具有spring-boot,hibernate,liquidbase,maven和angular4。我自己负责升级该项目(因为许多技术都是过时的版本),我陷入了困境。试图解决这个错误。 我对缓存一无所知,也不知道配置文件的作用,但是我相信错误在于DatabaseConfiguration.java和CacheConfiguration.java文件。

使用mvn spring-boot:run运行后端后,我收到此错误

我不确定要尝试什么或如何解决此问题,但是我在CacheConfiguration.java中查看了一下,发现它告诉我要添加的类(com.nissan.rca.domain.OCIOMacroTypeOfService)缓存已添加到缓存管理器中。

这是错误:

2019-02-07 14:58:20.013  WARN 9392 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.spri
ngframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error cr
eating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$Web
MvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to ins
tantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.fa
ctory.UnsatisfiedDependencyException: Error creating bean with name 'auditEventsEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/audit/AuditEventsEndp
ointAutoConfiguration.class]: Unsatisfied dependency expressed through method 'auditEventsEndpoint' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyEx
ception: Error creating bean with name 'customAuditEventRepository' defined in file [D:\RCA\ResourceCalenderApplication\target\classes\com\nissan\rca\repository\CustomAuditEventRepository.cl
ass]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenc
eAuditEventRepository': Cannot create inner bean '(inner bean)#1411479e' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1411479e': Cannot resolve reference to bean 'entityManagerFactory' while set
ting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [o
rg/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: All Hibernate caches should be created upfront. Please update CacheConfiguration.jav
a to add com.nissan.rca.domain.OCIOMacroTypeOfService
2019-02-07 14:58:20.270 ERROR 9392 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

这是CacheConfiguration.java

package com.nissan.rca.config;
import java.time.Duration;
import org.ehcache.config.builders.*;
import org.ehcache.jsr107.Eh107Configuration;
import io.github.jhipster.config.jcache.BeanClassLoaderAwareJCacheRegionFactory;
import io.github.jhipster.config.JHipsterProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.*;

@Configuration
@EnableCaching
// @AutoConfigureAfter(value = { MetricsConfiguration.class })
@AutoConfigureBefore(value = { WebConfigurer.class, DatabaseConfiguration.class })
public class CacheConfiguration {

    private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;

    public CacheConfiguration(JHipsterProperties jHipsterProperties) {
        BeanClassLoaderAwareJCacheRegionFactory.setBeanClassLoader(this.getClass().getClassLoader());
        JHipsterProperties.Cache.Ehcache ehcache =
            jHipsterProperties.getCache().getEhcache();

        jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
                ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
                .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
                .build());
    }

    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        return cm -> {
            cm.createCache(com.nissan.rca.repository.UserRepository.USERS_BY_LOGIN_CACHE, jcacheConfiguration);
            cm.createCache(com.nissan.rca.repository.UserRepository.USERS_BY_EMAIL_CACHE, jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.User.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Authority.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.User.class.getName() + ".authorities", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ResourceEngagement.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ResourceEngagement.class.getName() + ".ongoingResourceBudgetings", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ResourceEngagement.class.getName() + ".projectResourceBudgetings", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Resource.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Resource.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ResourceEngagementType.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ResourceEngagementType.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.EVPLevel.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.EVPLevel.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Vendor.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Vendor.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.TechnologyPlatform.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.TechnologyPlatform.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Role.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Role.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Section.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Section.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Location.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Location.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OCIOTechnologySoftware.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OCIOTechnologySoftware.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OCIOTypeOfService.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OCIOTypeOfService.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OCIOMacroTypeOfService.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OCIOMacroTypeOfService.class.getName() + ".oCIOTypeOfServices", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Currency.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Currency.class.getName() + ".resourceEngagements", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Currency.class.getName() + ".currencyExchangeToEuros", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.UserAccess.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.UserAccess.class.getName() + ".resources", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.FinancialYear.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.FinancialYear.class.getName() + ".currencyExchangeToEuros", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.FinancialYear.class.getName() + ".projectBudgets", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.FinancialYear.class.getName() + ".ongoingBudgets", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Project.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Project.class.getName() + ".projectBudgets", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OngoingBudget.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OngoingBudget.class.getName() + ".ongoingResourceBudgetings", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OngoingBudget.class.getName() + ".ongoingSpendItems", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ValueType.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ValueType.class.getName() + ".ongoingResourceBudgetings", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ValueType.class.getName() + ".ongoingSpendItems", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ValueType.class.getName() + ".projectResourceBudgetings", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ValueType.class.getName() + ".projectSpendItems", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.SpendType.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.SpendType.class.getName() + ".ongoingResourceBudgetings", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.SpendType.class.getName() + ".ongoingSpendItems", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.SpendType.class.getName() + ".projectResourceBudgetings", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.SpendType.class.getName() + ".projectSpendItems", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Month.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Month.class.getName() + ".ongoingResourceBudgetings", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Month.class.getName() + ".ongoingSpendItems", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Month.class.getName() + ".projectResourceBudgetings", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.Month.class.getName() + ".projectSpendItems", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ProjectBudget.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ProjectBudget.class.getName() + ".projectResourceBudgetings", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ProjectBudget.class.getName() + ".projectSpendItems", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.CostCentre.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.CostCentre.class.getName() + ".projects", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.CostCentre.class.getName() + ".ongoingBudgets", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ProjectPhase.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ProjectPhase.class.getName() + ".projects", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.CurrencyExchangeToEuro.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.SpendItem.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.SpendItem.class.getName() + ".ongoingSpendItems", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.SpendItem.class.getName() + ".projectSpendItems", jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OngoingResourceBudgeting.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.OngoingSpendItem.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ProjectSpendItem.class.getName(), jcacheConfiguration);
            cm.createCache(com.nissan.rca.domain.ProjectResourceBudgeting.class.getName(), jcacheConfiguration);
            // jhipster-needle-ehcache-add-entry
        };
    }
}

这是DatabseConfiguration.java

package com.nissan.rca.config;

import io.github.jhipster.config.JHipsterConstants;
import io.github.jhipster.config.liquibase.AsyncSpringLiquibase;

import liquibase.integration.spring.SpringLiquibase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;

@Configuration
@EnableJpaRepositories("com.nissan.rca.repository")
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
@EnableTransactionManagement
public class DatabaseConfiguration {

    private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);

    private final Environment env;

    public DatabaseConfiguration(Environment env) {
        this.env = env;
    }

    /**
     * Open the TCP port for the H2 database, so it is available remotely.
     *
     * @return the H2 database TCP server
     * @throws SQLException if the server failed to start
     */
    @Bean(initMethod = "start", destroyMethod = "stop")
    @Profile(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)
    public Object h2TCPServer() throws SQLException {
        try {
            // We don't want to include H2 when we are packaging for the "prod" profile and won't
            // actually need it, so we have to load / invoke things at runtime through reflection.
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            Class<?> serverClass = Class.forName("org.h2.tools.Server", true, loader);
            Method createServer = serverClass.getMethod("createTcpServer", String[].class);
            return createServer.invoke(null, new Object[] { new String[] { "-tcp", "-tcpAllowOthers" } });

        } catch (ClassNotFoundException | LinkageError  e) {
            throw new RuntimeException("Failed to load and initialize org.h2.tools.Server", e);

        } catch (SecurityException | NoSuchMethodException e) {
            throw new RuntimeException("Failed to get method org.h2.tools.Server.createTcpServer()", e);

        } catch (IllegalAccessException | IllegalArgumentException e) {
            throw new RuntimeException("Failed to invoke org.h2.tools.Server.createTcpServer()", e);

        } catch (InvocationTargetException e) {
            Throwable t = e.getTargetException();
            if (t instanceof SQLException) {
                throw (SQLException) t;
            }
            throw new RuntimeException("Unchecked exception in org.h2.tools.Server.createTcpServer()", t);
        }
    }

    @Bean
    public SpringLiquibase liquibase(@Qualifier("taskExecutor") TaskExecutor taskExecutor,
            DataSource dataSource, LiquibaseProperties liquibaseProperties) {

        // Use liquibase.integration.spring.SpringLiquibase if you don't want Liquibase to start asynchronously
        SpringLiquibase liquibase = new AsyncSpringLiquibase(taskExecutor, env);
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog("classpath:config/liquibase/master.xml");
        liquibase.setContexts(liquibaseProperties.getContexts());
        liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema());
        liquibase.setDropFirst(liquibaseProperties.isDropFirst());
        if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_NO_LIQUIBASE)) {
            liquibase.setShouldRun(false);
        } else {
            liquibase.setShouldRun(liquibaseProperties.isEnabled());
            log.debug("Configuring Liquibase");
        }
        return liquibase;
    }
}

这是错误中显示的类OCIOMacroTypeOfService.java

package com.nissan.rca.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;

/**
 * A OCIOMacroTypeOfService.
 */
@Entity
@Table(name = "ociomacrotypeofservice")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class OCIOMacroTypeOfService implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ocio_macro_type_code")
    private String ocioMacroTypeOfServiceCode;

    @Column(name = "ocio_macro_type_description")
    private String ocioMacroTypeDescription;

    @OneToMany(mappedBy = "ocioMacroTypeOfService")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<OCIOTypeOfService> oCIOTypeOfServices = new HashSet<>();
    // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
    public String getOcioMacroTypeOfServiceCode() {
        return ocioMacroTypeOfServiceCode;
    }

    public void setOcioMacroTypeOfServiceCode(String ocioMacroTypeOfServiceCode) {
        this.ocioMacroTypeOfServiceCode = ocioMacroTypeOfServiceCode;
    }

    public String getOcioMacroTypeOfServiceDescription() {
        return ocioMacroTypeDescription;
    }

    public OCIOMacroTypeOfService ocioMacroTypeOfServiceDescription(String ocioMacroTypeDescription) {
        this.ocioMacroTypeDescription = ocioMacroTypeDescription;
        return this;
    }

    public void setOcioMacroTypeOfServiceDescription(String ocioMacroTypeDescription) {
        this.ocioMacroTypeDescription = ocioMacroTypeDescription;
    }

    public Set<OCIOTypeOfService> getOCIOTypeOfServices() {
        return oCIOTypeOfServices;
    }

    public OCIOMacroTypeOfService oCIOTypeOfServices(Set<OCIOTypeOfService> oCIOTypeOfServices) {
        this.oCIOTypeOfServices = oCIOTypeOfServices;
        return this;
    }

    public OCIOMacroTypeOfService addOCIOTypeOfService(OCIOTypeOfService oCIOTypeOfService) {
        this.oCIOTypeOfServices.add(oCIOTypeOfService);
        oCIOTypeOfService.setOcioMacroTypeOfService(this);
        return this;
    }

    public OCIOMacroTypeOfService removeOCIOTypeOfService(OCIOTypeOfService oCIOTypeOfService) {
        this.oCIOTypeOfServices.remove(oCIOTypeOfService);
        oCIOTypeOfService.setOcioMacroTypeOfService(null);
        return this;
    }

    public void setOCIOTypeOfServices(Set<OCIOTypeOfService> oCIOTypeOfServices) {
        this.oCIOTypeOfServices = oCIOTypeOfServices;
    }
    // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        OCIOMacroTypeOfService oCIOMacroTypeOfService = (OCIOMacroTypeOfService) o;
        if (oCIOMacroTypeOfService.getOcioMacroTypeOfServiceCode() == null || getOcioMacroTypeOfServiceCode() == null) {
            return false;
        }
        return Objects.equals(getOcioMacroTypeOfServiceCode(), oCIOMacroTypeOfService.getOcioMacroTypeOfServiceCode());
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getOcioMacroTypeOfServiceCode());
    }

    @Override
    public String toString() {
        return "OCIOMacroTypeOfService{" +
            "ocioMacroTypeOfServiceCode=" + getOcioMacroTypeOfServiceCode() +
            ", ocioMacroTypeOfServiceDescription='" + getOcioMacroTypeOfServiceDescription() + "'" +
            "}";
    }
}

0 个答案:

没有答案