我有以下jpa实体:
实体
@Entity
@Table(name = "todo")
@NamedEntityGraph(name = "TodoEntity.children",
attributeNodes = @NamedAttributeNode("children"))
public class TodoEntity {
public TodoEntity() {
}
@Id
@GeneratedValue
private Long id;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Long userId;
private String text;
private String description;
private Boolean collapsed;
private Boolean completed;
private Integer sortOrder;
private Date expiredDate;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
private Integer priority;
private String guid;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@JsonIgnore
@Column(updatable = false,nullable = false)
private Long parentId;
@OneToMany(fetch = FetchType.EAGER)
@Cascade({CascadeType.ALL})
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "parentId", referencedColumnName = "id")
@OrderBy("sortOrder ASC")
private List<TodoEntity> children;
}
回购
@EntityGraph(value = "TodoEntity.children", type = EntityGraph.EntityGraphType.FETCH)
Page<TodoEntity> findByUserIdAndParentId(Long userId, Long parentId, Pageable pageable);
我想要一个我可以无限嵌套的todolist。到目前为止,代码还在运行——唯一的问题是,当我有大约1500个TODO并且它们是嵌套的时,sql和查询会变得很慢(大约1,3秒)
我该如何改进?
想法:
实际上,我认为只要用一个查询(需要2,2 ms)获取数据库中的所有todo,然后用java(具有parentid)嵌套它们就足够了,这样工作负载就在应用层上了。
暂无答案!
目前还没有任何答案,快来回答吧!