Liquibase:MySqlSyntaxErrorException:表已经存在

时间:2015-04-16 07:11:33

标签: hibernate jpa spring-boot liquibase

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

我有一个Spring启动应用程序,我使用Jhipster创建了实体,另外我自己添加了一些手动更改日志更改集。

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

@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更改日志中提及,而不是在医生中。

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

2 个答案:

答案 0 :(得分:7)

虽然我仍然不确定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">...

答案 1 :(得分:1)

您可能已经在@Table(name =“ Table_Name”)的Hibernate批注中提到了表名,并且必须已经存储了它。因此,要解决此问题,您可以删除数据文件夹的所有内容,然后重新启动服务器并尝试运行您的应用程序。

为我工作,因为我以前没有执行过脚本。