我是Spring data的新手,我想知道如何解决以下情况:
我有一个 Product 类,它有一个 Model 类的属性,这个子类有一个属性“price”。 我的目标是有一个方法返回按“价格”订购的所有产品,但由于属性价格在子类模型而不是类产品中,我不知道该怎么做。
以前我直接在类产品中获得价格,所以在我的 JPARepository 中我有:
public interface ProductRepository extends JpaRepository<Product,Long> {
List<Product> findByOrderByPriceDesc();
List<Product> findByOrderByPriceAsc();
}
但现在由于属性价格已移至类模型,我不知道如何修改这些方法的签名以使其适应数据模型的变化。
你能帮帮我吗?
最佳答案
我现在在家试了一下。
它按预期工作:
findByPropertyOrderBySubclassProperty(Property property);
我创建了一个示例项目并将其放在 Github 上:https://github.com/Yannic92/stackOverflowExamples/tree/master/SpringBoot/OrderByNestedSubclassProperty
如果这有帮助,请告诉我:)
关于java - Spring 数据 - 如何按子类中的属性排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33068494/