1. 程式人生 > >JPA中id前面有空格導致的"Column 'id' not found"問題

JPA中id前面有空格導致的"Column 'id' not found"問題

問題背景

昨晚有個同事發生了一個神奇的問題,一如既往的問題,一如既然的用我寫的BEJSON-JAVA程式碼生成器生成,卻發現一直提示Column ‘id’ not found。這就很TM神奇了

2018-12-29 11:06:19.574  WARN 332172 --- [nio-7778-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: S0022
2018-12-29 11:06:19.574 ERROR 332172 --- [nio-7778-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'id' not found.
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select * from gene_sample_inventory where  sample_num = ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
	at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:242)
	at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
	at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
	at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:6

解決方案

網上說mysql-connect-java版本太低,於是從5.1升到了8.0都沒用。

程式碼看起來簡直是完全沒問題的。。。

再後來。。。看多了一眼,,,id前面有空格,,,所以這個主鍵其實是 空格id,我的天,這樣mysql竟然可以通過。於是乎,問題解決了。

在這裡插入圖片描述

試驗了一下,還真的是可以打空格。
在這裡插入圖片描述