user26873585
user26873585

Reputation: 11

Error creating bean with name 'liquibase' defined in class path resource Problem

I want to apply Liquibase in my Spring Boot Project but It doesn't work. The error is

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Circular depends-on relationship between 'liquibase' and 'entityManagerFactory'
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307) ~[spring-beans-6.1.4.jar:6.1.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.1.4.jar:6.1.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:312) ~[spring-beans-6.1.4.jar:6.1.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.1.4.jar:6.1.4]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1231) ~[spring-context-6.1.4.jar:6.1.4]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:949) ~[spring-context-6.1.4.jar:6.1.4]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624) ~[spring-context-6.1.4.jar:6.1.4]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.2.3.jar:3.2.3]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.2.3.jar:3.2.3]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.2.3.jar:3.2.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:334) ~[spring-boot-3.2.3.jar:3.2.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354) ~[spring-boot-3.2.3.jar:3.2.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-3.2.3.jar:3.2.3]
    at com.dife.api.DifeApplication.main(DifeApplication.java:12) ~[main/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.2.3.jar:3.2.3]

My application.yml file is

spring:
    jpa:
        database-platform: org.hibernate.dialect.MySQLDialect
        show-sql: true
        hibernate:
            ddl-auto: update
        defer-datasource-initialization: false

    liquibase:
        enabled: true
        change-log: classpath:db/changelog/changelog-master.xml

    main:
        allow-circular-references: true

    sql:
        init:
          mode: always
          data-locations: classpath:import.sql


... I connected my datasource

changelog-master.xml (resources/db/changelog/changelog-master.xml)

I wrote this xml file based on the official document

https://contribute.liquibase.com/extensions-integrations/directory/integration-docs/springboot/#__tabbed_1_2

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

    <changeSet id="1" author="my_name">
    </changeSet>

</databaseChangeLog>

build.gradle

implementation 'org.liquibase:liquibase-core'

I tried database all dropped and

based on googling, added on my application.yml

spring.jpa.defer-datasource-initialization: false
spring.jpa.main.allow-circular-reference: true

But the same errors happened... Could you tell me what's the cause of error and solution to solve it?

Upvotes: 1

Views: 111

Answers (1)

veljkost
veljkost

Reputation: 1932

In your app configuration (application.yml) you are using both hibernate and liquibase as a method of initializing and maintaining DB schema, which is not the way to go as it can create schema conflicts or unpredictable state of the database.

If you are going with liquibase as a source of truth for your database schema state, disable hibernate's ddl-auto by setting it to none. This way hibernate will not make any DDL changes to your database schema and will leave it to liquibase to maintain db migrations.

If you are just starting with adding liquibase to your existing project you can create an initial changelog based on your current database state by using generateChangeLog from the command line, as explained here https://stackoverflow.com/a/47809416/3535298

Upvotes: 0

Related Questions