目录
  • Spring Data JPA踩坑记录
  • JPA踩坑:No property xxx found for type xxx
    • 问题发现
    • 问题解决

Spring Data JPA踩坑记录

最近在做自己的一个项目时 使用了spring jpa

由于数据库用的是mysql 在给实体类entity 的id给注解时@Id遇到了一个坑 自己找了许久才在stackoverflow 上找到了答案

注意 再查询数据库的时候并不会因此报错 而当你的主键是自增的时候 在添加数据的时候就会报错了

Spring Data JPA踩坑记录(@id @GeneratedValue)

Spring Data JPA踩坑记录(@id @GeneratedValue)

看看我们的实体类

注意id

Spring Data JPA踩坑记录(@id @GeneratedValue)

原因是id jpa默认是auto的策略 也就是主键序列化 而mysql是不支持的 oracle支持的 所有在添加的时候才会报错

所以我们要主动设置id的策略

Spring Data JPA踩坑记录(@id @GeneratedValue)

这是坑 特意写出来 让大家(主要是使用jpa新手)不用像我一样踩坑了 注意了

JPA踩坑:No property xxx found for type xxx

问题发现

今天调试一段代码的时候发现有个报错很奇葩。

接口报错:

{
    "errorCode": "01",
    "errorMessage": "服务器出错",
    "returnObject": [
        "Parameter value [1531421824] did not match expected type [java.util.Date (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [1531421824] did not match expected type [java.util.Date (n/a)]"
    ]
}

后台一直报错:

org.springframework.data.mapping.PropertyReferenceException: No property xxxx found for type Xxxx

问题解决

最后发现是同事写JPA@Repository的时候,@Query里面的东西写错了

//这个是错误的,里面是value不是name=。=尴尬
@Query(name="select * from gene_info where  to_days(createtime )= to_days( FROM_UNIXTIME( '?1' ) ) ",nativeQuery=true)
    List<GeneInfo> findAll(String createtime);
//正确的是@Query(value=" xxx",nativeQuery=true)
@Query(value="select * from gene_info where  to_days(createtime )= to_days( FROM_UNIXTIME( '?1' ) ) ",nativeQuery=true)
    List<GeneInfo> findAll(String createtime);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。