Java 排序jpa嵌套实体
我在Spring Java JPA中有以下实体:Java 排序jpa嵌套实体,java,spring,hibernate,spring-data-jpa,jpql,Java,Spring,Hibernate,Spring Data Jpa,Jpql,我在Spring Java JPA中有以下实体: @Entity @Table(name = "todo") public class TodoEntity { @Id @GeneratedValue private Long id; @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private Long userId; private String text; pri
@Entity
@Table(name = "todo")
public class TodoEntity {
@Id
@GeneratedValue
private Long id;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Long userId;
private String text;
private String description;
private Boolean expanded;
private Boolean completed;
private Integer sortOrder;
private Date expiredDate;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date updateDate;
private Integer priority;
private Long parentId;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "parentId", referencedColumnName = "id")
private Set<TodoEntity> children;
.....getter and setter
}
@实体
@表(name=“todo”)
今天的公共课{
@身份证
@生成值
私人长id;
@JsonProperty(access=JsonProperty.access.WRITE_ONLY)
私有长用户ID;
私有字符串文本;
私有字符串描述;
私有布尔扩展;
完成私有布尔运算;
私有整数排序器;
私有日期到期日;
@CreationTimestamp
@时态(TemporalType.TIMESTAMP)
私人约会;
@UpdateTimestamp
@时态(TemporalType.TIMESTAMP)
私人日期更新日期;
私有整数优先级;
私人长父ID;
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinColumn(name=“parentId”,referencedColumnName=“id”)
私人儿童;
……接二连三
}
现在我的问题是孩子们的分类。
我想对它们进行排序,有时按日期,有时按排序器(因此基本上使用不同的参数)
如何在JPA内部做到这一点
信息:
- 使用SpringJPA数据存储库
@Repository
public interface TodoRepository extends JpaRepository<TodoEntity, Long> {
Page<TodoEntity> findByUserIdAndParentIdAndTextContaining(Long userId, Long parentId, String text, Pageable pageable);
Page<TodoEntity> findByUserIdAndParentId(Long userId, Long parentId, Pageable pageable);
TodoEntity findByUserIdAndId(Long userId, Long id);
}
@存储库
到存储库的公共接口扩展了JpaRepository{
页面FindByUserAndParentId和TextContaining(长用户ID、长家长ID、字符串文本、可分页);
Page FINDBYUSERIANDPARENTID(长用户ID、长家长ID、可分页);
TodoEntity findByUserIdAndId(长用户id,长id);
}
让我们以findbyuserialandparentid为例使用jpa查询并动态生成order by子句您使用的是spring数据存储库吗?是的。使用jparepositoryshow您的存储库,可能是一种查询方法,您希望对其排序
findByUserIdAndIdOrderByChildern\u SortOrder(长用户id,长id)代码>否很遗憾,这不起作用。也许是为了让事情更清楚。我希望所有的孩子都被正确地分类。现在,OneToMany中的孩子是随机排序的。我希望它们按排序器、creationDate或updateDate进行排序