错误
Hibernate: select order0_.id as id1_0_, order0_.amount as amount2_0_, order0_.buyer_address as buyer_ad3_0_, order0_.buyer_mobile as buyer_mo4_0_, order0_.buyer_name as buyer_na5_0_, order0_.buyer_openid as buyer_op6_0_, order0_.create_time as create_t7_0_, order0_.pay_status as pay_stat8_0_, order0_.status as status9_0_, order0_.update_time as update_10_0_ from order order0_ where order0_.buyer_openid=? limit ?
2021-04-03 16:27:16.607 WARN 9024 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
2021-04-03 16:27:16.607 ERROR 9024 --- [ main] 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 'order order0_ where order0_.buyer_openid='1234567890' limit 6' at line 1
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at xxx.xxx.xxx
这种错误有两种情况,第一种就是数据表与数据库的关键字重名了,第二种就是entity对象的属性和数据库关键字重名了,很显然,我两种方式都中招了…
我们可以在数据库表数据与Java对象的对应类(也就是我们常说的entity)上面使用
@Table(name = "`数据表名称`")
来指定数据表名称,使用
@Column(name = "`数据表字段`")
来指定数据表的字段,我们知道在MySQL数据库使用的时候,如果数据表或者字段和数据库中的关键字重名,虽然有时可以操作数据库,但是有的时候却不可以,这时候我们可以使用着重号" `` " 包裹要起的名字就可以了。
这个错误的解决完了,在我们使用Spring-data-jpa的时候,虽然它可以自动生成SQL语句很方便,但是生成的SQL可能会执行报错,所以要在使用的时候要细心一点。