JPA使用过程中遇到的问题

MySQL结合过程问题

Hibernate命名策略更改表名

Spring Spring Data JPA 错误: Page 1 of 1 containing UNKNOWN instances

JPA Pageable查询索引是从0开始,前端传入page值默认为1。

Page taskPage=workTaskService.queryTaskPage(taskName,scheStrategy,taskStatus,
                new PageRequest(page-1,limit));

最开始我以为只有这一个问题,后来还是报错,检查代码后发现又被自己蠢死了。

之前代码

 @Query("select  new com.ustc.inspectweb.entity.ro.WorkTaskMonitorRo(" +
            "a.workTaskId,a.devType,a.endDate,a.taskName,a.taskDesc," +
            "a.startDate,a.taskStatus,a.scheStrategy,a.nextExecuteTime) " +
            "from WorkTaskEntity a where a.isDelete=0 " +
            " and (a.taskName is null  or a.taskName like concat('%',:taskName,'%'))" +
            " and (a.scheStrategy is null  or a.scheStrategy=:scheStrategy )" +
            " and (a.taskStatus is null or a.taskStatus=:taskStatus)" +
            " order by a.startDate desc")

更改之后

 @Query("select  new com.ustc.inspectweb.entity.ro.WorkTaskMonitorRo(" +
            "a.workTaskId,a.devType,a.endDate,a.taskName,a.taskDesc," +
            "a.startDate,a.taskStatus,a.scheStrategy,a.nextExecuteTime) " +
            "from WorkTaskEntity a where a.isDelete=0 " +
            " and (:taskName is null  or a.taskName like concat('%',:taskName,'%'))" +
            " and (:scheStrategy is null  or a.scheStrategy=:scheStrategy )" +
            " and (:taskStatus is null or a.taskStatus=:taskStatus)" +
            " order by a.startDate desc")

taskName会传入null

日期转换JSON格式问题

@RestControllerDate类型转换为JSON时,格式不正确有两种解决办法:

  • 在application.properties配置修改全局设置,对所有Date类型都生效
  • 给指定类型添加注解@JsonFormat,仅对被注解的属性生效

两个方法都有,注解优先级高。

application.properties配置

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.serialization.write-dates-as-timestamps=false #值为true表示用long型时间戳

注解设置

long型的timestamp表示日期:

@JsonFormat(shape = JsonFormat.Shape.NUMBER)

字符串表示日期:

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")

desc保留字问题

场景:新建一个实体类,里面有一个desc字段,结果在保存的时候,一直报错SQL Error: 1064, SQLState: 42000

解决方法:https://stackoverflow.com/questions/28946567/sql-error-1064-sqlstate-42000-while-creating-new-entity

更改字段名称_

https://etherealbits.com/2012/12/debunking-the-myths-of-rpc-rest/

你可能感兴趣的:(Spring)