Table doesn't exist:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

随声附和 提交于 2020-01-03 05:30:28

问题


Entity 1:

@Entity
@Table(name = "META_INF")
@Inheritance(strategy = InheritanceType.JOINED)
public class META_INF {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotBlank
    @Column(nullable = false)
    private Boolean aa;

    @ManyToOne
    private USER bb;

    @NotBlank
    @JoinColumn(nullable = false)
    @ManyToOne
    private USER cc;

    @NotBlank
    @Column(nullable = false)
    private Boolean dd;

    private String ee;

    @NotBlank
    @Column(nullable = false)
    private Boolean ff;

    @NotBlank
    @JoinColumn(nullable = false, name = "tt")
    @ManyToOne
    private AZE tt;


}

Entity 2:

@Entity
@Table(name = "QWE", uniqueConstraints = {
        @UniqueConstraint(columnNames = { "a", "b", "c", "d" }) })
public class QWE extends META_INF {

    @NotBlank
    @JoinColumn(nullable = false, name = "a")
    @ManyToOne
    private PIL_P_TRATMNT a;

    @NotBlank
    @Column(nullable = false, name = "b")
    private String b;

    @NotBlank
    @Min(value = 0)
    @Digits(integer = 3, fraction = 0)
    @Column(nullable = false, name = "c")
    private int c;

    @NotBlank
    @Min(value = 0)
    @Digits(integer = 3, fraction = 0)
    @Column(nullable = false, name = "d")
    private int d;

    @NotBlank
    @Column(nullable = false, name = "e")
    private String e;

    @Column(name = "f")
    private String f;


    @NotBlank
    @Column(nullable = false, name = "g")
    private String g;

    @NotBlank
    @Column(nullable = false, name = "h")
    private String h;

    @NotBlank
    @Column(nullable = false, name = "i")
    private String i;

    @Digits(integer = 2, fraction = 0)
    @Column(name = "j")
    private int j;

    public QWE () {
    }   

}

and lot of other entities..

application.properties to specify the credentials to access to the database:

jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/transverse
jdbc.username = root
jdbc.password = root
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto=create

the database is empty. when I try to run the SpringMVC app, it gives me this error ( tables are created in DB ):

WARN : org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl - GenerationTarget encountered exception accepting command : Unable to execute command [
    alter table QWE 
        drop 
        foreign key FK5f4s5qg3vma47jthit51i7wvy]
org.hibernate.tool.schema.spi.CommandAcceptanceException: Unable to execute command [
    alter table QWE 
        drop 
        foreign key FK5f4s5qg3vma47jthit51i7wvy]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:63)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:370)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:355)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applyConstraintDropping(SchemaDropperImpl.java:327)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:229)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:153)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:125)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:111)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:137)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:64)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:458)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:416)
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:401)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187)
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:198)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'transverse.qwe' doesn't exist

i got the same error for all other entities


回答1:


You mentioned:

the database is empty. when I try to run the SpringMVC app, it gives me this error.

It is expected that you get this error when the database is empty. Here is what will happen when you specify hbm2ddl.auto=create.

 --Execute the scripts in this order
 alter table <TABLE NAME> drop foreign key <CONSTRAINT NAME>;

 drop table <TABLE NAME> if exists

 create table <TABLE NAME>

 alter table <TABLE NAME> add constraint <CONSTRAINT NAME>    

When hibernate runs the scripts with these settings the very first time, the errors which are detected since the constriants are missing will be logged as warnings. The DROP scripts will not fail since they have the if exists check.

On a second run the constraints will exist and the alter table drop constraint statements will not fail. Hence you will not see any errors.



来源:https://stackoverflow.com/questions/36927420/table-doesnt-existcom-mysql-jdbc-exceptions-jdbc4-mysqlsyntaxerrorexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!