在JPA查询中排序返回Child对象

时间:2022-07-07 00:18:34

So if my JPA query is like this: Select distinct p from Parent p left join fetch p.children order by p.someProperty

因此,如果我的JPA查询是这样的:从父p中选择不同的p左边连接由p.someProperty获取p.children顺序

I correctly get results back ordered by p.someProperty, and I correctly get my p.children collection eagerly fetched and populated. But I'd like to have my query be something like "order by p.someProperty, p.children.someChildProperty" so that the collection populated inside each parent object was sub-ordered by someChildProperty.

我正确地得到了p.someProperty排序的结果,并且我正确地获取并填充了我的p.children集合。但我希望我的查询类似于“按p.someProperty,p.children.someChildProperty排序”,以便填充每个父对象内部的集合由someChildProperty进行子排序。

This seems intuitive when I think in terms of the sql that is actually generated for these calls, but I guess less so when it tries to map back to hierarchical objects.

当我考虑实际为这些调用生成的sql时,这看起来很直观,但是当我尝试映射回分层对象时,我猜的情况就更少了。

2 个解决方案

#1


For preserving order, use TreeSet. As far as, sorting of a collection inside parent is concerned, just do it in your code using Comparator.

要保留顺序,请使用TreeSet。至于父类内部集合的排序,只需使用Comparator在代码中进行。

Well, try this on your collection definition in your parent entity class. I hope you are getting my point.

好吧,在父实体类的集合定义中尝试这个。我希望你明白我的观点。

You can use this JPA annotation,

你可以使用这个JPA注释,

@javax.persistence.OrderBy(value = "fieldName")

or this Hibernate specific,

或者这个特定的Hibernate,

@org.hibernate.annotations.OrderBy(clause = "FIELD_NAME asc")

and you can also use this,

你也可以用这个,

@org.hibernate.annotations.Sort(type = SortType.NATURAL)

or

@org.hibernate.annotations.Sort(type = SortType.COMPARATOR)

In the case of comparator, a comparator must be in place. Other might only work with String collections.

在比较器的情况下,必须有比较器。其他可能只适用于String集合。

#2


Adeel basically has this nailed. You can also use those with a List which might be important if your collections contain the same entity more than once.

阿黛尔基本上已经钉了这个。如果您的集合多次包含同一个实体,您也可以使用那些可能很重要的List。

#1


For preserving order, use TreeSet. As far as, sorting of a collection inside parent is concerned, just do it in your code using Comparator.

要保留顺序,请使用TreeSet。至于父类内部集合的排序,只需使用Comparator在代码中进行。

Well, try this on your collection definition in your parent entity class. I hope you are getting my point.

好吧,在父实体类的集合定义中尝试这个。我希望你明白我的观点。

You can use this JPA annotation,

你可以使用这个JPA注释,

@javax.persistence.OrderBy(value = "fieldName")

or this Hibernate specific,

或者这个特定的Hibernate,

@org.hibernate.annotations.OrderBy(clause = "FIELD_NAME asc")

and you can also use this,

你也可以用这个,

@org.hibernate.annotations.Sort(type = SortType.NATURAL)

or

@org.hibernate.annotations.Sort(type = SortType.COMPARATOR)

In the case of comparator, a comparator must be in place. Other might only work with String collections.

在比较器的情况下,必须有比较器。其他可能只适用于String集合。

#2


Adeel basically has this nailed. You can also use those with a List which might be important if your collections contain the same entity more than once.

阿黛尔基本上已经钉了这个。如果您的集合多次包含同一个实体,您也可以使用那些可能很重要的List。