[英]I want to apply Max() function on database using seedstack with repository
I am trying to get max value of float column using SeedStack Java code where I am using seedstack Repository.我正在尝试使用 SeedStack Java 代码获取浮点列的最大值,其中我使用的是 Seedstack Repository。 But I am not able understand how to do it.
但我不明白该怎么做。 As repository do not max function to be called.
作为存储库,不要调用最大函数。 it has the only count function.
它具有唯一的计数功能。 I have applied the filters using Specification.
我已经使用规范应用了过滤器。 But got stuck with Max function.
但是被Max函数卡住了。 can anyone help?
有人可以帮忙吗?
I can not share the code due to restrictions.由于限制,我无法共享代码。
The repository abstraction allows you to manage aggregate persistence with a collection-like facade but generic methods can only return objects of the type of the aggregate.存储库抽象允许您使用类似集合的外观来管理聚合持久性,但泛型方法只能返回聚合类型的对象。
When doing data aggregation like in your case, you don't return an aggregate but individual columns and aggregation results.在像您的情况一样进行数据聚合时,您不会返回聚合,而是返回单个列和聚合结果。 This is not compatible with generic Repository methods and Specification objects.
这与通用存储库方法和规范对象不兼容。
To achieve what you want, you have to define a custom repository method.为了实现你想要的,你必须定义一个自定义存储库方法。 Define it in a custom interface:
在自定义接口中定义它:
public interface ProductRepository extends Repository<Product, Long> {
int getMaximumProductPrice();
}
Then implement it:然后实现它:
public class ProductJpaRepository
extends BaseJpaRepository<Product, Long>
implements ProductRepository {
@Override
public int getMaximumProductPrice() {
// implement query using getEntityManager()
}
}
Now you inject your repository with your own interface directly:现在,您可以直接使用自己的界面注入存储库:
public class SomeClass {
@Inject
private SomeRepository someRepository;
}
The example above is very simple but note that you can also return more complex values like:上面的示例非常简单,但请注意,您还可以返回更复杂的值,例如:
Object
to hold arbitrary values,Object
数组,Product
or list of Product
if you do a join.Product
或列表Product
,如果你做一个加盟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.