snow-swallow / blog

Seek more? Go to Issues :-)
1 stars 0 forks source link

JPA相关坑 #103

Open snow-swallow opened 2 years ago

snow-swallow commented 2 years ago
  1. 一般只在一边设Eager,JPA接口默认为一对多为Lazy,多对一为Eager,但是Hibernate反向工程生成Entity时,多对一为Lazy,需要手动改为Eager。 https://blog.csdn.net/tongyang8820/article/details/71157948

  2. ManyToOne循环引用问题

    Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.hvisions.auth.entity.SysUser["properties"]->org.hibernate.collection.internal.PersistentBag[

    一端 @JsonManagedReference 多端 // @JsonBackReference // @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "groupId") @JsonIgnoreProperties("users")

  3. JPA返回元组 No converter found capable of converting from type

  4.  Illegal DefaultValue null for parameter type integer @ApiModelProperty(name = "id", value = "id", example = "1") // 需要添加example

  5. ManyToOne 外键无法更新 @JoinColumn(name = "groupId", insertable = true, updatable = true)

  6. 动态拼接JPA自带的Query 的技巧

    @Query(value = "SELECT ua FROM User ua "
            + "WHERE ua.targetName LIKE %?1% "
            + "and (?2 is null or ua.createTime > ?2) "
            + "and (?3 is null or ua.createTime <= ?3) "
            + "and (?4 is null or ua.action = ?4) ")
    public Page<User> findByActionAndTargetNameAndCreateTime(String targetName, Date startTime, Date endTime, String action, Pageable page);
  7. request parameter 设置成required,并且定义默认值

            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false) Integer size,
  8. 聚合查询,用原生jpa query没法好好做,改成nativeQuery,返沪对象是一个map

    @Query(value = "SELECT g.id, g.group_name AS groupName, count(u.id) AS totalUserCount "
            + "FROM admin_group g LEFT JOIN admin_user u ON g.id = u.group_id "
            + "WHERE g.id in (:groupIds) GROUP BY g.id", nativeQuery = true)
    List<Map<String, Object>> findAllGroup(@Param("groupIds") List<Integer> groupIds);
  9. Based on above item, additional pagination features can be added on with countQuery:

    ##https://blog.csdn.net/weixin_45415885/article/details/101571356
    @Query(value = "SELECT * FROM  emp " +
       "WHERE  if(?1= '' OR ?1 is null ,1=1, code like %?1%) " +
       " AND if(?2= '' OR ?2 is null ,1=1, `name` like %?2%) " +
       " GROUP BY dept_id ",
       countQuery = "SELECT count(*) FROM (SELECT count(*) FROM emp "+
       "WHERE  if(?1= '' OR ?1 is null ,1=1, code like %?1%) " +
       " AND if(?2= '' OR ?2 is null ,1=1, `name` like %?2%) " +
       "GROUP BY dept_id ) tempTable",
       nativeQuery = true)
    Page<Map> findEmp( String code,  String name);

    alternatives: http://jinzili.cc/2017/10/11/Spring-Data-JPA%E4%B8%AD%E7%9A%84nativeQuery%E5%92%8Cpageable/

    @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1 \n#pageable\n", nativeQuery = true)
    Page<User> findByEmailAddress(String emailAddress, Pageable pageable);
snow-swallow commented 2 years ago

TODO:

  1. define enum on request parameters in the swagger doc
  2. find out useful constraint of swagger
snow-swallow commented 2 years ago

feign client 不能初始化Page接口的数据 org.springframework.data.domain.Page` (no Creators, like default construct,