【问题标题】:spring-boot, spring-boot-starter-data-jpa: database type NONE or Cannot find changelog locationspring-boot,spring-boot-starter-data-jpa:数据库类型无或找不到更改日志位置
【发布时间】:2015-06-18 02:59:03
【问题描述】:

我又遇到了一个 spring-boot-starter-data-jpa 问题。我使用 spring-boot 访问多个数据库。 MongoDB 做对了,Cassandra 也做对了,最后一个 PostgreSQL 和 JPA 有一些问题。我认为我的配置可能是错误的。你能帮我弄清楚吗?我使用了 spring-boot、spring-data-jpa 参考和 spring-boot-starter-data-jpa 参考。没有任何帮助。我在 stakeoverflow 上发现了一些线程,它们使用 jdbc 和 hsql 依赖项,因为 jpa 无法正常运行Link。我自己试过这个,但我得到一个没有合格的 bean 错误。

所以我退了一步,包括 jpa 和 hsql,编写了一个 PostgresConfig 类,没有任何帮助。如果我使用 @EnableJpaRepositories 注释,我会收到一个有线错误。请参阅 Stacktrace Para 67.5.2 错误...我没有使用这个 liquibase 东西,我知道为什么会出现错误...否则,如果我不使用 DataSourceBuilder,我的 application.properties 将不会被读取并且我得到了数据库类型无错误。

一些来源: 堆栈跟踪:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot find changelog location: class path resource [db/changelog/db.changelog-master.yaml] (please add changelog or check your Liquibase configuration)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at com.kage.bigdata.bida.Application.main(Application.java:22)
Caused by: java.lang.IllegalStateException: Cannot find changelog location: class path resource [db/changelog/db.changelog-master.yaml] (please add changelog or check your Liquibase configuration)
    at org.springframework.util.Assert.state(Assert.java:385)
    at org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration.checkChangelogExists(LiquibaseAutoConfiguration.java:83)
    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:483)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:349)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:300)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
    ... 26 more

application.properties:

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.schema=bigdata

spring.datasource.platform=postgresql
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.database=postgresql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=drop-create

配置:

package com.kage.bigdata.bida.config;

import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
 *
 * @author bl4ckbird
 */
@Configuration
@EnableJpaRepositories("com.kage.bigdata.bida.repository.postgres")
public class PostgresConfig {

    @Bean
    public DataSource dataSource()  {    
           return DataSourceBuilder.create().build();

    }


}

POM.XML:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kage.bigdata</groupId>
    <artifactId>bida</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.3.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>

    </dependency>

    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>

    </dependency>
    <dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <scope>runtime</scope>
</dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-cassandra</artifactId>

    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.2</version>
        <scope>provided</scope>
    </dependency>

</dependencies>
</project>

应用:

package com.kage.bigdata.bida;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 * @author bl4ckbird
 */
@SpringBootApplication


public class Application {

    public static void main(String[] args) throws Throwable { 
        SpringApplication app = new SpringApplication(Application.class); 
        app.run(); 
    } 
} 

存储库:

package com.kage.bigdata.bida.repository.postgres;

import com.kage.bigdata.bida.model.Question;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 *
 * @author bl4ckbird
 */
public interface QPostgresRepository extends JpaRepository<Question, String> {

}

QuestionEntity(多注解(cassandra,mongo,jpa):

package com.kage.bigdata.bida.model;


import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;

/**
 *
 * @author bl4ckbird
 */
@Table
@Entity
public class Question implements Serializable{
    @Id
    @PrimaryKey
    @Getter
    @Setter
    @GeneratedValue(strategy = GenerationType.AUTO)
    @javax.persistence.Id
    private String id;
    @Getter
    @Setter
    private String question;



    @Override
    public String toString() {
        return String.format(
                "Question[id=%s, Question='%s']",
                id, question);
    }
}

自动接线服务:

package com.kage.bigdata.bida.service.impl;

import com.kage.bigdata.bida.model.Question;

import com.kage.bigdata.bida.repository.cassandra.QCassandraRepository;
import com.kage.bigdata.bida.repository.QuestionRepository;
import com.kage.bigdata.bida.repository.postgres.QPostgresRepository;

import com.kage.bigdata.bida.service.QuestionService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author bl4ckbird
 */

@Service
public class QuestionServiceImpl implements QuestionService{

    @Autowired
    QuestionRepository questionRepository;

    @Autowired
    QCassandraRepository cassandraRepository;

    @Autowired
    QPostgresRepository postgresRepository;

    public QuestionServiceImpl(){};

    @Override
    @Transactional
    public void test() {
        Question q = new Question();
        q.setQuestion("Frage");

        questionRepository.save(q);

        System.out.println(questionRepository.findAll()); 



        q.setQuestion("Frage2");


        cassandraRepository.save(q);

        System.out.println(cassandraRepository.findAll());
    }

}

【问题讨论】:

    标签: spring spring-boot spring-data-jpa


    【解决方案1】:

    开始工作了!最后......我做了什么:从 pom deb 中删除 HSQLDB。将liquidbaseautoconfiguration.class 排除在外...... 1 行代码和8 小时代码阅读......这里是我的配置类。希望我能帮助别人:

    @Configuration
    @EnableAutoConfiguration(exclude = 
                LiquibaseAutoConfiguration.class
              )
    @EnableJpaRepositories("com.kage.bigdata.bida.repository.postgres")
    public class PostgresConfig {
    
    
        @Bean
        public DataSource dataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("org.postgresql.Driver");
            dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/postgres");
            dataSource.setUsername("postgres");
            dataSource.setPassword("postgres");
    
            return dataSource;
        }
    
        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
            LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
            entityManagerFactoryBean.setDataSource(dataSource());
            entityManagerFactoryBean.setPackagesToScan("com.kage.bigdata.bida.model");
            entityManagerFactoryBean.setJpaProperties(buildHibernateProperties());
            entityManagerFactoryBean.setJpaProperties(new Properties() {{
                put("hibernate.current_session_context_class", SpringSessionContext.class.getName());
            }});
            entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter() {{
                setDatabase(Database.POSTGRESQL);
            }});
            return entityManagerFactoryBean;
        }
    
    
    
        protected Properties buildHibernateProperties()
        {
            Properties hibernateProperties = new Properties();
    
            hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
            hibernateProperties.setProperty("hibernate.show_sql", "true");
            hibernateProperties.setProperty("hibernate.use_sql_comments", "false");
            hibernateProperties.setProperty("hibernate.format_sql", "false");
            hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    
            hibernateProperties.setProperty("hibernate.generate_statistics", "false");
    
            hibernateProperties.setProperty("javax.persistence.validation.mode", "none");
    
            //Audit History flags
            hibernateProperties.setProperty("org.hibernate.envers.store_data_at_delete", "true");
            hibernateProperties.setProperty("org.hibernate.envers.global_with_modified_flag", "true");
    
            return hibernateProperties;
        }
    
        @Bean
        public PlatformTransactionManager transactionManager() {
            return new JpaTransactionManager();
        }
    
        @Bean
        public TransactionTemplate transactionTemplate() {
            return new TransactionTemplate(transactionManager());
        }
    
    }
    

    【讨论】:

    • Spring Boot 只会为您配置 Liquibase,前提是它位于类路径中,在这种情况下,它是 spring-data-cassandra 的传递依赖项。如果你不使用它,你可能想尝试在你的 pom 中排除它。
    • 如您所见,我做到了,但没有任何地方写到 cassandra 对 liquibase 具有传递依赖。让我花 8 个小时来解决这个问题……我认为 jpa 具有传递依赖性,因为在我包含 spring-data-jpa 后问题变得活跃
    • 感谢您的提示。查看依赖项: mvn dependency:tree
    【解决方案2】:

    您必须像这样从 string-data-cassandra 依赖项中排除 liquibase:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-cassandra</artifactId>
        <version>1.2.0.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2021-01-18
      • 2021-02-09
      • 2021-11-11
      • 2017-11-29
      • 1970-01-01
      • 2018-08-06
      • 2020-05-21
      • 2018-03-01
      • 2018-03-01
      相关资源
      最近更新 更多