spring data jpa使用错误记录

来源:互联网 发布:转行程序员 编辑:程序博客网 时间:2024/06/13 10:02

一、调用JpaRepository save保存数据时,报错

Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; xxx at line x 

原因:保存数据的时候,sql语法错误,但sql是jpa自动生成的,不存在写错,经过分析发现使用了sql的关键字condition做字段。

二、调用JpaRepository save保存数据时,报错

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'appversion0_.app_version' in 'field list'    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)    at com.mysql.jdbc.Util.getInstance(Util.java:408)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)

原因:我这里@cloumn配置字段并没有配错,但数据库字段为驼峰命名appVersion,hibenate默认使用的命名策略是下划线分隔的字段命名。添加以下配置,更改策略为无修改命名。

无修改命名spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 

备注:

遇到大写字母 加”_”的命名spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

三、数据库表ID为自增列,调用JpaRepository save,同时获取返回实体对象,发现id为0,没有获取到自增数据

原因:实体id字段需添加主键生成策略注解,@GeneratedValue(strategy=GenerationType.IDENTITY)

四、JpaRepository查询数据报错:Null value was assigned to a property of primitive type setter …

原因:实体字段类型使用一些列的原始类型,但是这些字段在数据库中是空的。

解决方案一:使用包装类(Integer for int …)
解决方案二:定义列的默认值。

原创粉丝点击