我已经看到了几个相关的问题及其答案,但这对我的情况没有帮助。

我有一个Spring Boot应用程序,在其中我使用Jhipster创建了实体,另外我还添加了一些手动的changelog变更集。

我有一个Doctor实体,其中包含与“ Specialty”的一对多单向关系,其中“ Doctor”是关系的所有者。因此,我指定了一个联接表,该表将存储Doctor和Specialty外键。

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(
        name="T_DOCTOR_SPECIALTY",
        joinColumns = @JoinColumn( name="doctor_id"),
        inverseJoinColumns = @JoinColumn( name="specialty_id")
)
private List<Specialty> specialties = new ArrayList<>();


为此,我添加了一个变更日志文件,该文件甚至包含用于忽略专业的标记(如果已经存在),

<changeSet id="20150415081455" author="waqas">
    <preConditions onFail="MARK_RAN">
        <not>
            <tableExists tableName="T_DOCTOR_SPECIALTY"/>
        </not>
    </preConditions>
</changeSet>

<changeSet id="20150415081221" author="waqas">

    <createTable tableName="T_DOCTOR_SPECIALTY">

        <column name="id" type="bigint" autoIncrement="true">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="doctor_id" type="bigint">
            <constraints nullable="false" />
        </column>
        <column name="specialty_id" type="bigint">
            <constraints nullable="false" />
        </column>
    </createTable>

    <addForeignKeyConstraint baseColumnNames="doctor_id"
                             baseTableName="T_DOCTOR_SPECIALTY"
                             constraintName="fk_doctor_id"
                             referencedColumnNames="id"
                             referencedTableName="T_DOCTOR"/>

    <addForeignKeyConstraint baseColumnNames="specialty_id"
                             baseTableName="T_DOCTOR_SPECIALTY"
                             constraintName="fk_specialty_id"
                             referencedColumnNames="id"
                             referencedTableName="T_SPECIALTY"/>
</changeSet>


然后,我创建一个没有表的新数据库并运行该应用程序,但出现以下异常:

(Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' is defined)
[ERROR] org.springframework.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'liquibase' defined in class path resource [io/aurora/ams/config/DatabaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set classpath:config/liquibase/changelog/20150415081221_added_entity_Doctor_Specialty.xml::20150415081221::waqas:
 Reason: liquibase.exception.DatabaseException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 't_doctor_specialty' already exists
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.2.3.RELEASE.jar:1.2.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) ~[spring-boot-1.2.3.RELEASE.jar:1.2.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) ~[spring-boot-1.2.3.RELEASE.jar:1.2.3.RELEASE]
at io.aurora.ams.Application.main(Application.java:64) [bin/:na]

Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set classpath:config/liquibase/changelog/20150415081221_added_entity_Doctor_Specialty.xml::20150415081221::waqas:
 Reason: liquibase.exception.DatabaseException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 't_doctor_specialty' already exists
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:586) ~[liquibase-core-3.3.2.jar:na]
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:43) ~[liquibase-core-3.3.2.jar:na]
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:73) ~[liquibase-core-3.3.2.jar:na]
at liquibase.Liquibase.update(Liquibase.java:200) ~[liquibase-core-3.3.2.jar:na]
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:353) ~[liquibase-core-3.3.2.jar:na]
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:317) ~[liquibase-core-3.3.2.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]

Caused by: liquibase.exception.DatabaseException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 't_doctor_specialty' already exists
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:316) ~[liquibase-core-3.3.2.jar:na]
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55) ~[liquibase-core-3.3.2.jar:na]
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:122) ~[liquibase-core-3.3.2.jar:na]
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1227) ~[liquibase-core-3.3.2.jar:na]
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1210) ~[liquibase-core-3.3.2.jar:na]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:550) ~[liquibase-core-3.3.2.jar:na]

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 't_doctor_specialty' already exists
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_40]
at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_40]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.Util.getInstance(Util.java:360) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:848) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:742) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.zaxxer.hikari.proxy.StatementProxy.execute(StatementProxy.java:83) ~[HikariCP-2.2.5.jar:na]
at com.zaxxer.hikari.proxy.StatementJavassistProxy.execute(StatementJavassistProxy.java) ~[HikariCP-2.2.5.jar:na]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:314) ~[liquibase-core-3.3.2.jar:na]


我要提及的是,仅在20150415081221_added_entity_Doctor_Specialty.xml更改日志中提到了该关系,而在Doctor中没有提及。

我进行了很多搜索,但不确定是什么原因造成的。任何帮助都感激不尽。

最佳答案

尽管我仍然不确定与Liquibase有关此错误的确切问题是什么,但是由于存在约束,使用preCondition标记的解决方法无法正常工作,在Liquibase文档中提到:

“在变更集之外(例如,在变更日志的开头),只有HALT和WARN是可能的值。”

我缺少在createTable之前将preCondition放置在changeSet中。

<changeSet id="20150415081221" author="waqas">
    <preConditions onFail="MARK_RAN">
        <not>
            <tableExists tableName="T_DOCTOR_SPECIALTY"/>
        </not>
    </preConditions>
    <createTable tableName="T_DOCTOR_SPECIALTY">...

关于hibernate - Liquibase:MySqlSyntaxErrorException:表已存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29667760/

10-12 01:36