【发布时间】:2020-08-29 16:47:48
【问题描述】:
我有办法
List<Notification> findTop20ByToUserIdAndIdGreaterThanAndOrderByIdDesc(Long ToUserId, Long lastId);
我有通知列表,我想用这个方法:
-
选择发送到“toUserId”的通知
-
选择最后添加的 20 个
-
按 Desc 对 20 进行排序
-
并选择大于(我提供给方法的 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