简体   繁体   中英

Spring Boot HIbernate Issue: Error executing DDL via JDBC Statement with ddl-auto=create-drop

I am a novice with Spring and Hibernate and have tried everything I could find to fix this with no luck. Any assistance would be greatly appreciated. I am currently following a sample project in a book, and am getting an error that says "GenerationTarget encountered exception accepting command: Error executing DDL via JDBC Statement." I am using a MariaDB database, and my hibernate dialect is org.hibernate.dialect.MySQL5InnoDBDialect.

The query causing this issue is

alter table car drop foreign key FK2mqqwvxtowv4vddvtsmvtiqa2;

If I try to run this query directly in the database it works fine. I have the ddl-auto property set to create-drop, so maybe it is trying to execute this query when the car table has been dropped and is not there? I'm not sure why it would do that or how to turn off this behavior if that is the problem.

If I change ddl-auto to validate or update, the application fails to run at all. If I change it to create, it works fine and I get no errors.

With create-drop, the application does connect to the database and all my tables are created. The tables are still defined as they should be, but the error remains.

Console output:

 2018-07-25 16:56:24.229 INFO 12414 --- [ restartedMain] c.p.cardatabase.CardatabaseApplication: Starting CardatabaseApplication on Northstar-2.local with PID 12414 2018-07-25 16:56:24.230 INFO 12414 --- [ restartedMain] c.p.cardatabase.CardatabaseApplication: No active profile set, falling back to default profiles: default 2018-07-25 16:56:24.278 INFO 12414 --- [ restartedMain] ConfigServletWebServerApplicationContext: Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@42e3f007: startup date [Wed Jul 25 16:56:24 AKDT 2018]; root of context hierarchy 2018-07-25 16:56:25.134 INFO 12414 --- [ restartedMain] osbfsDefaultListableBeanFactory: Overriding bean definition for bean 'httpRequestHandlerAdapter' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration; factoryMethodName=httpRequestHandlerAdapter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; factoryMethodName=httpRequestHandlerAdapter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]] 2018-07-25 16:56:25.535 INFO 12414 --- [ restartedMain] trationDelegate$BeanPostProcessorChecker: Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$e5303d26] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-07-25 16:56:25.850 INFO 12414 --- [ restartedMain] osbwembedded.tomcat.TomcatWebServer: Tomcat initialized with port(s): 8080 (http) 2018-07-25 16:56:25.875 INFO 12414 --- [ restartedMain] o.apache.catalina.core.StandardService: Starting service [Tomcat] 2018-07-25 16:56:25.875 INFO 12414 --- [ restartedMain] org.apache.catalina.core.StandardEngine: Starting Servlet Engine: Apache Tomcat/8.5.29 2018-07-25 16:56:25.883 INFO 12414 --- [ost-startStop-1] oacatalina.core.AprLifecycleListener: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/tatiana/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.] 2018-07-25 16:56:26.025 INFO 12414 --- [ost-startStop-1] oac.c.C.[Tomcat].[localhost].[/]: Initializing Spring embedded WebApplicationContext 2018-07-25 16:56:26.025 INFO 12414 --- [ost-startStop-1] osweb.context.ContextLoader: Root WebApplicationContext: initialization completed in 1750 ms 2018-07-25 16:56:26.155 INFO 12414 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean: Mapping filter: 'characterEncodingFilter' to: [/*] 2018-07-25 16:56:26.156 INFO 12414 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean: Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-07-25 16:56:26.156 INFO 12414 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean: Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-07-25 16:56:26.156 INFO 12414 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean: Mapping filter: 'requestContextFilter' to: [/*] 2018-07-25 16:56:26.156 INFO 12414 --- [ost-startStop-1].s.DelegatingFilterProxyRegistrationBean: Mapping filter: 'springSecurityFilterChain' to: [/*] 2018-07-25 16:56:26.156 INFO 12414 --- [ost-startStop-1] osbwservlet.ServletRegistrationBean: Servlet dispatcherServlet mapped to [/] 2018-07-25 16:56:26.288 INFO 12414 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource: HikariPool-1 - Starting... 2018-07-25 16:56:26.354 INFO 12414 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource: HikariPool-1 - Start completed. 2018-07-25 16:56:26.385 INFO 12414 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean: Building JPA container EntityManagerFactory for persistence unit 'default' 2018-07-25 16:56:26.401 INFO 12414 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper: HHH000204: Processing PersistenceUnitInfo [ name: default...] 2018-07-25 16:56:26.467 INFO 12414 --- [ restartedMain] org.hibernate.Version: HHH000412: Hibernate Core {5.2.16.Final} 2018-07-25 16:56:26.469 INFO 12414 --- [ restartedMain] org.hibernate.cfg.Environment: HHH000206: hibernate.properties not found 2018-07-25 16:56:26.504 INFO 12414 --- [ restartedMain] o.hibernate.annotations.common.Version: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 2018-07-25 16:56:26.602 INFO 12414 --- [ restartedMain] org.hibernate.dialect.Dialect: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect 2018-07-25 16:56:27.062 WARN 12414 --- [ restartedMain] ohtsiExceptionHandlerLoggedImpl: GenerationTarget encountered exception accepting command: Error executing DDL via JDBC Statement org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.tool.schema.internal.SchemaDropperImpl.applyConstraintDropping(SchemaDropperImpl.java:331) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:230) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:144) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:312) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:460) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892) [hibernate-core-5.2.16.Final.jar:5.2.16.Final] at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) [spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) [spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) [spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) [spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) [spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1761) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1698) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] at com.packt.cardatabase.CardatabaseApplication.main(CardatabaseApplication.java:28) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.0.1.RELEASE.jar:2.0.1.RELEASE] Caused by: java.sql.SQLSyntaxErrorException: (conn=342) Table 'cardb.car' doesn't exist at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:177) ~[mariadb-java-client-2.2.3.jar:na] at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:110) ~[mariadb-java-client-2.2.3.jar:na] at org.mariadb.jdbc.MariaDbStatement.executeExceptionEpilogue(MariaDbStatement.java:228) ~[mariadb-java-client-2.2.3.jar:na] at org.mariadb.jdbc.MariaDbStatement.executeInternal(MariaDbStatement.java:334) ~[mariadb-java-client-2.2.3.jar:na] at org.mariadb.jdbc.MariaDbStatement.execute(MariaDbStatement.java:386) ~[mariadb-java-client-2.2.3.jar:na] at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95) ~[HikariCP-2.7.8.jar:na] at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-2.7.8.jar:na] at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]... 40 common frames omitted Caused by: java.sql.SQLException: Table 'cardb.car' doesn't exist Query is: alter table car drop foreign key FK2mqqwvxtowv4vddvtsmvtiqa2 at org.mariadb.jdbc.internal.util.LogQueryTool.exceptionWithQuery(LogQueryTool.java:119) ~[mariadb-java-client-2.2.3.jar:na] at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.executeQuery(AbstractQueryProtocol.java:199) ~[mariadb-java-client-2.2.3.jar:na] at org.mariadb.jdbc.MariaDbStatement.executeInternal(MariaDbStatement.java:328) ~[mariadb-java-client-2.2.3.jar:na]... 44 common frames omitted [... Mapping details here, omitted] 2018-07-25 16:56:27.118 INFO 12414 --- [ restartedMain] ohtschema.internal.SchemaCreatorImpl: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@23352c6f' 2018-07-25 16:56:27.121 INFO 12414 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean: Initialized JPA EntityManagerFactory for persistence unit 'default' 2018-07-25 16:56:28.047 INFO 12414 --- [ restartedMain] ossweb.DefaultSecurityFilterChain: Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@7ec73b7e, org.springframework.security.web.context.SecurityContextPersistenceFilter@6f233313, org.springframework.security.web.header.HeaderWriterFilter@1e641fa3, org.springframework.security.web.csrf.CsrfFilter@7c5a0312, org.springframework.security.web.authentication.logout.LogoutFilter@176619be, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@26add7c1, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@4030f929, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@2ea06fad, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@6ce594ae, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@6159e3c1, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@51157300, org.springframework.security.web.session.SessionManagementFilter@5be9c504, org.springframework.security.web.access.ExceptionTranslationFilter@4506e3d3, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@1f44843c] org.springframework.http.HttpEntity<org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.ProfileController.listAllFormsOfMetadata() 2018-07-25 16:56:28.632 INFO 12414 --- [ restartedMain] osbdaOptionalLiveReloadServer: LiveReload server is running on port 35729 2018-07-25 16:56:28.668 INFO 12414 --- [ restartedMain] osjeaAnnotationMBeanExporter: Registering beans for JMX exposure on startup 2018-07-25 16:56:28.670 INFO 12414 --- [ restartedMain] osjeaAnnotationMBeanExporter: Bean with name 'dataSource' has been autodetected for JMX exposure 2018-07-25 16:56:28.676 INFO 12414 --- [ restartedMain] osjeaAnnotationMBeanExporter: Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource] 2018-07-25 16:56:28.712 INFO 12414 --- [ restartedMain] osbwembedded.tomcat.TomcatWebServer: Tomcat started on port(s): 8080 (http) with context path '' 2018-07-25 16:56:28.715 INFO 12414 --- [ restartedMain] c.p.cardatabase.CardatabaseApplication: Started CardatabaseApplication in 4.726 seconds (JVM running for 5.165)

Here is the code on my repo: https://github.com/Kallaste/spring_login_server_example_so

I wouldn't worry about it if your application still runs. Hibernate throws a lot of 'informative' errors on startup. It just tries to figure out what it has to do to get the database in a state where your application can run.

If the app runs fine if you use create-drop or just create , then try running it with create just once so everything is generated and then change it to validate so the next time it only validates your database schema.

In a production environment you would generally not let Hibernate update the database schema by itself but manually apply changes and migrate data anyway, unless you're very sure the changes would be applied correctly.

You should use create the first time you run your application, but the next runs have to have update .

When you use create you create the schema and destroy the previous data. And create-drop destroy the schema in the end of the session. update only update you schema.

update should still be valid. If tables don't exist, it will behave as create.

Are you sure your DB and DBDialect match?

Add @Table annotation like below

@Table(name="car") in car entity. Always give name to table.

Note:

1) When ddl-auto=create-drop and if you don't have @Table(name="car") always you will get this exception.

2) When ddl-auto=create-drop and if you have @Table(name="car") the first time alone you will get this exception.

So try to change the ddl-auto value to other than create-drop. May be you can use "create" or "update"

Hope this helps.

Need to change spring.jpa.hibernate.ddl-auto = update instead of create-drop in application.properties file.
Because It is dropping the database at the start of the application and so it won't find the required events table to alter anything.

The problem is because you don't have permissions on the database user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM