2016-12-14 28 views
0

以下のエラーの解決には本当に疲れました。どんな助力も非常に感謝しています。SQLエラー:1064、SQLState:42000 - 原因:org.hibernate.exception.SQLGrammarException:ステートメントを実行できませんでした

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:242) ~[spring-orm-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225) ~[spring-orm-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) ~[spring-orm-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131) ~[spring-data-jpa-1.9.2.RELEASE.jar:na] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at com.sun.proxy.$Proxy69.saveAndFlush(Unknown Source) ~[na:na] 
    at com.boot.controller.ShipwreckController.create(ShipwreckController.java:30) ~[classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101] 
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101] 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_101] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_101] 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.30.jar:8.0.30] 
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101] 

私はリンクをたどって、それらのどれも私のために働いていません。 私はMYSQLでSpring Bootを開発しています。

Shipwreck.java

@Entity 
public class Shipwreck { 
    @Id 
    /*@GeneratedValue(strategy = GenerationType.AUTO)*/ 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    Long id; 
    String shipwreckName; 
    String description; 
    String condition; 
    Integer depth; 
    Double latitude; 
    Double longitude; 
    Integer yearDiscovered; 

    public Shipwreck() { } 

    public Shipwreck(Long id, String shipwreckName, String description, String condition, Integer depth, Double latitude, Double longitude, Integer yearDiscovered) { 
     this.id = id; 
     this.shipwreckName = shipwreckName; 
     this.description = description; 
     this.condition = condition; 
     this.depth = depth; 
     this.latitude = latitude; 
     this.longitude = longitude; 
     this.yearDiscovered = yearDiscovered; 
    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getShipwreckName() { 
     return shipwreckName; 
    } 

    public void setShipwreckName(String shipwreckName) { 
     this.shipwreckName = shipwreckName; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getCondition() { 
     return condition; 
    } 

    public void setCondition(String condition) { 
     this.condition = condition; 
    } 

    public Integer getDepth() { 
     return depth; 
    } 

    public void setDepth(Integer depth) { 
     this.depth = depth; 
    } 

    public Double getLatitude() { 
     return latitude; 
    } 

    public void setLatitude(Double latitude) { 
     this.latitude = latitude; 
    } 

    public Double getLongitude() { 
     return longitude; 
    } 

    public void setLongitude(Double longitude) { 
     this.longitude = longitude; 
    } 

    public Integer getYearDiscovered() { 
     return yearDiscovered; 
    } 

    public void setYearDiscovered(Integer yearDiscovered) { 
     this.yearDiscovered = yearDiscovered; 
    } 
} 

application.properties

logging.level.org.springframework.web=DEBUG 

server.port=8080 

spring.h2.console.enabled=true 
spring.h2.console.path=/h2 

## For H2 DB 
#spring.datasource.url=jdbc:h2:file:~/dasboot 
#spring.datasource.username=sa 
#spring.datasource.password= 
#spring.datasource.driver-class-name=org.h2.Driver 

## For MYSQL DB 
spring.datasource.url=jdbc:mysql://localhost:3306/dasboot 
spring.datasource.username=root 
spring.datasource.password=root 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

spring.datasource.max-active=10 
spring.datasource.max-idle=8 
spring.datasource.max-wait=10000 
spring.datasource.min-evictable-idle-time-millis=1000 
spring.datasource.min-idle=8 
spring.datasource.time-between-eviction-runs-millis=1 

flyway.baseline-on-migrate=true 
spring.jpa.hibernate.ddl-auto=update; 

# The SQL dialect makes Hibernate generate better SQL for the chosen database 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 

#datasource.flyway.url=jdbc:h2:file:~/dasboot 
#datasource.flyway.username=sa 
#datasource.flyway.password= 
#datasource.flyway.driver-class-name=org.h2.Driver 


datasource.flyway.url=jdbc:mysql://localhost:3306/dasboot 
datasource.flyway.username=root 
datasource.flyway.password=root 
datasource.flyway.driver-class-name=com.mysql.jdbc.Driver 

編集-1:

2016-12-16 11:47:59.988 DEBUG 6156 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Read [class com.boot.model.Shipwreck] as "application/json;charset=UTF-8" with [org.springfr[email protected]1869eff] 
Hibernate: insert into shipwreck (condition, depth, description, latitude, longitude, shipwreckname, year_discovered) values (?, ?, ?, ?, ?, ?, ?) 
2016-12-16 11:48:00.481 WARN 6156 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000 
2016-12-16 11:48:00.482 ERROR 6156 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition, depth, description, latitude, longitude, shipwreckname, year_discover' at line 1 
2016-12-16 11:48:00.484 WARN 6156 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: 1064, SQLState: 42000 
2016-12-16 11:48:00.484 WARN 6156 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition, depth, description, latitude, longitude, shipwreckname, year_discover' at line 1 
2016-12-16 11:48:00.494 DEBUG 6156 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [public com.boot.model.Shipwreck com.boot.controller.ShipwreckController.create(com.boot.model.Shipwreck)]: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement 
2016-12-16 11:48:00.496 DEBUG 6156 --- [nio-8080-exec-1] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [public com.boot.model.Shipwreck com.boot.controller.ShipwreckController.create(com.boot.model.Shipwreck)]: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement 
2016-12-16 11:48:00.496 DEBUG 6156 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [public com.boot.model.Shipwreck com.boot.controller.ShipwreckController.create(com.boot.model.Shipwreck)]: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement 
2016-12-16 11:48:00.612 DEBUG 6156 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet  : Could not complete request 

答えて

1

OK、問題がありますconditionです。の予約語はhereで入手できます(またはウェブサイトから別のバージョンを入手できます)。

経験的には、org.hibernate.exception.SQLGrammarExceptionを取得した場合は、予約語に問題があることがあります。

もう1つのヒントは、SQLを表示し、データベースで直接SQLを実行しようとすることです、それはSQLの問題を見つけることがより簡単になります。

春の起動時にspring.jpa.show-sql=trueを使用すると、休止状態のSQLを表示できます。

+0

は、情報をありがとうございました。今でもログを更新しました。 PlsはEdit-1を参照してください。 –

+0

@ user4798115はい、あなたのエンティティの 'condition'プロパティはMySQLの**予約語ですので、プロパティ名を更新するか、' @ Column'を使って別の列名を定義してください。 –

+0

ありがとうございました –

0

条件はMySQLでは予約語なので、エラーです。

変数をアプリケーションのモデルでshipCondtionに変更します。

は、私はあなたにもフライウェイ・マイグレーション・スクリプトを作成しているだろうと推測:

create table shipwreck(
    `id` int(10) NOT NULL AUTO_INCREMENT, 
    `name` varchar(255), 
    `description` varchar(255), 
    `ship_condition` varchar(255), 
    `depth` int(10), 
    `latitude` double(17,4), 
    `longitude` double(17,4), 
    `year_discovered` int(4), 
    PRIMARY KEY(id) 
); 

これは現在正常に動作する必要があります。

あなたがapplication.propertiesにMySQLDialectを定義する必要はありません:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 
関連する問題