简体   繁体   English

如何检索按 ID 分组的所有实体?

[英]How to retrieve all Entities Grouped By Id?

I am using spring-jpa and using entity manager for persistence.我正在使用 spring-jpa 并使用实体管理器进行持久性。

I am developing a spring-application and trying to retrieve all the entities that have persisted before a certain time grouping them on their Id.我正在开发一个弹簧应用程序并尝试检索在某个时间之前一直存在的所有实体,并将它们分组到他们的 ID 上。

Tried the following query inside a createQuery using entitymanager使用 entitymanager 在 createQuery 中尝试了以下查询

"Select se From SampleEntity se where se.createdDate > :elapsedTime group by se.uid"

The model looks like this: model 看起来像这样:

class SampleEntity
{

@Id
private String uid;      //Cannot be null

@Id
private String uid2;     //Should be unique, 

//also combo of uid, uid2 should be unique

private String name;

private Date createdDate;

private Date updatedDate;

}

Assume the following data in db假设db中有以下数据

Uid     Uid2     Name     CreatedDate            UpdatedDate
A-123   X-123     AX      10/6/2019 10:00:00AM   10/6/2019 10:00:00AM
A-123   X-124     AX      10/6/2019 10:00:20AM   10/6/2019 10:00:20AM
B-124   Y-125     BY      10/6/2019 10:01:00AM   10/6/2019 10:01:00AM
B-124   Y-126     BY      10/6/2019 10:01:20AM   10/6/2019 10:01:20AM

Actual result:实际结果:

Keep getting error "not a group by expression"不断收到错误“不是按表达式分组”

Expected:预期的:

I want the entity to be retrived as groups of Uid.我希望将实体作为 Uid 组进行检索。

So, I want to be able to query and retrieve a list of 2 objects where each object in turn would be a list/array of entities that have the same uid.因此,我希望能够查询和检索 2 个对象的列表,其中每个 object 依次是具有相同 uid 的实体列表/数组。

Object 1 - would contain 2 sample entity objects that would contain data from the following 2 rows: A-123 X-123 AX 10/6/2019 10:00:00AM 10/6/2019 10:00:00AM A-123 X-124 AX 10/6/2019 10:00:20AM 10/6/2019 10:00:20AM Object 1 - 将包含 2 个样本实体对象,其中将包含来自以下 2 行的数据: A-123 X-123 AX 10/6/2019 10:00:00AM 10/6/2019 10:00:00AM A-123 X -124 AX 2019 年 10 月 6 日上午 10:00:20 2019 年 10 月 6 日上午 10:00:20

Object 2 - would contain 2 sample entity objects that would contain data from the following 2 rows: B-124 Y-125 BY 10/6/2019 10:01:00AM 10/6/2019 10:01:00AM B-124 Y-126 BY 10/6/2019 10:01:20AM 10/6/2019 10:01:20AM Object 2 - 将包含 2 个样本实体对象,其中将包含来自以下 2 行的数据: B-124 Y-125 BY 10/6/2019 10:01:00AM 10/6/2019 10:01:00AM B-124 Y -126 2019 年 10 月 6 日上午 10:01:20 2019 年 10 月 6 日上午 10:01:20

Is this possible?这可能吗? If yes, how?如果是,如何? What am I missing?我错过了什么?

When using GROUP BY only the grouping columns or aggregates function (COUNT...) can be used in the SELECT clause, just as with SQL queries.当使用 GROUP BY 时,只有分组列或聚合 function (COUNT...) 可以在 SELECT 子句中使用,就像 SQL 查询一样。 This example would be a correct query, it would result in a list of uid.这个例子将是一个正确的查询,它会产生一个 uid 列表。

Select se.uid From SampleEntity se where se.createdDate > :elapsedTime group by se.uid

However, his query will not allow you to retrieve a list of list of objects, as SQL group by does not work this way, and I don't think this is feasible with JPA.但是,他的查询不允许您检索对象列表,因为 SQL group by 不能以这种方式工作,而且我认为 JPA 不可行。 You would have to do a regular query and build the your nested lists in the service layer.您必须进行常规查询并在服务层中构建嵌套列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何强制休眠而不更新具有相同ID的所有实体? - How to force hibernate not update all entities with same id? 如何在流 groupingBy 之后聚合分组的实体 - How to aggregate grouped entities after stream groupingBy (JPQL)检索不在@OneToMany属性中的所有实体 - (JPQL) Retrieve all entities which are not in @OneToMany property HQL 查询以选择按与已建立实体相关的参数值分组的所有唯一实体 - HQL query to select all unique entities grouped by the parameter value that is in relation to founded entities 如何获取用户登录ID - how to retrieve the userLogin id 如何在单个查询中检索嵌套的 JPA 实体 - How to retrieve nested JPA entities in a single query 如何使用Pojo从数据存储中检索实体? - How to retrieve entities from datastore by using pojo? 如何检索相关实体的有限集合? - How to retrieve the limited collection of related entities? Spring如何在@Controller生成的json中保留所有子实体的实体ID? - Spring how to keep entity id of all sub entities within the json generated by @Controller? JUnit 测试检索所有实体失败,实际结果为“[]” - JUnit test to retrieve all entities fails with actual result '[]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM