【问题标题】:Spring Data JPA methodSpring Data JPA 方法
【发布时间】:2020-08-29 16:47:48
【问题描述】:

我有办法

List<Notification> findTop20ByToUserIdAndIdGreaterThanAndOrderByIdDesc(Long ToUserId, Long lastId);

我有通知列表,我想用这个方法:

  1. 选择发送到“toUserId”的通知

  2. 选择最后添加的 20 个

  3. 按 Desc 对 20 进行排序

  4. 并选择大于(我提供给方法的 lastId 的数量(出于更新原因))。

    这不起作用并抛出这样的异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'notificationRepo': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.bagenger.persistence.dao.NotificationRepo.findTop20ByToUserIdAndIdGreaterThanAndOrderByIdDesc(java.lang.Long)! No property greaterThanAnd found for type Long! Traversed path: Notification.id.
 My Entity:

@Entity
@Data
public class Notification {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;
    private Long fromUserId;
    private Long toUserId;
    private String url;
    
    @ColumnDefault("false")
    private boolean isRead;
    private String message;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private LocalDateTime createDate;
}

我该如何重写方法才能正常工作?

【问题讨论】:

  • 嵌套异常表示:greaterThanAnd 不是运算符。您在大于和 orderby 之间进行逻辑和,这是错误的。删除“和”
  • 这个版本也不行: 1. findTop20ByToUserIdAndIdGreaterThanIdOrderByIdDesc 2. findTop20ByToUserIdOrderByIdDescAndIdGreaterThan

标签: hibernate jpa methods spring-data-jpa spring-data


【解决方案1】:

看起来问题出在方法名称应该添加实体类名称:通知 这个正在工作:

List<Notification> findTop20NotificationByToUserIdAndIdGreaterThanOrderByIdDesc(Long ToUserId, Long lastId);

【讨论】:

    【解决方案2】:

    方法名称不符合标准。你可以试试下面的方法名。

    List<Notification> findTop20NoByToUserIdIsAndIdIsGreaterThanOrderByIdDesc(Long ToUserId, Long lastId);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 2020-02-09
      • 2017-08-20
      • 2018-06-25
      • 2013-11-14
      • 1970-01-01
      • 2018-08-25
      相关资源
      最近更新 更多