- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在SpringBoot微服务中,我试图为每个mean_of_payment_id选择一个 Actor 的最新记录。为此,请使用mean_of_payment_id上的group by子句为actor_id选择actor内容,其中created_date等于max(created_date)嵌套查询的子集。我正在使用JPQL。下面是表的结构和查询。
@Query("select ac from ActorContent ac "
+ "where (ac.actor.uuid=:actorUuid ) and "
+ "ac.createdDate IN ( SELECT MAX(aci.createdDate) "
+ "FROM ActorContent aci WHERE ac.actor.uuid=aci.actor.uuid "
+ "and aci.uuid = ac.uuid group by ac.meanOfPayment.id)"
)
最佳答案
我认为在关系代数方面,您要求的是ActorContent
的集合减去由actor = actor和meanOfPayment = meanOfPayment和createDate
ActorContent
与
ac1.meanOfPayment = ac2.meanOfPayment and ac1.actor = ac2.actor and ac1.createDate < ac2.createDate
的叉积获得第二组。然后从
ActorContent
的集合中减去该集合。我没有看过它是否比使用
MAX
和
Group By
更有效。例如:
@Query("select ac from ActorContent ac where ac.id not in (select ac1.id from ActorContent ac1, ActorContent ac2 where ac1.meanOfPayment = ac2.meanOfPayment and ac1.actor = ac2.actor and ac1.createDate < ac2.createDate)")
ActorContent [id=1, actor=Actor [id=1], meanOfPayment=MeanOfPayment [id=1], amount=10500.00, createDate=2018-10-09 00:00:00.887]
ActorContent [id=2, actor=Actor [id=2], meanOfPayment=MeanOfPayment [id=1], amount=-10400.00, createDate=2018-10-02 00:00:00.887]
ActorContent [id=3, actor=Actor [id=2], meanOfPayment=MeanOfPayment [id=3], amount=6000.00, createDate=2018-10-02 00:00:00.887]
ActorContent [id=4, actor=Actor [id=2], meanOfPayment=MeanOfPayment [id=2], amount=200.00, createDate=2018-09-30 00:00:00.887]
Actor
和
MeanOfPayment
实例来优化查询。举例来说:
@Query("select ac from ActorContent ac left outer join fetch ac.actor left outer join fetch ac.meanOfPayment where ac.id not in (select ac1.id from ActorContent ac1, ActorContent ac2 where ac1.meanOfPayment = ac2.meanOfPayment and ac1.actor = ac2.actor and ac1.createDate < ac2.createDate)")
select actorconte0_.id as id1_1_0_, actor1_.id as id1_0_1_, meanofpaym2_.id as id1_2_2_, actorconte0_.actor_id as actor_id4_1_0_, actorconte0_.amount as amount2_1_0_, actorconte0_.create_date as create_d3_1_0_, actorconte0_.mean_of_payment_id as mean_of_5_1_0_ from actor_content actorconte0_ left outer join actor actor1_ on actorconte0_.actor_id=actor1_.id left outer join mean_of_payment meanofpaym2_ on actorconte0_.mean_of_payment_id=meanofpaym2_.id where actorconte0_.id not in (select actorconte3_.id from actor_content actorconte3_ cross join actor_content actorconte4_ where actorconte3_.mean_of_payment_id=actorconte4_.mean_of_payment_id and actorconte3_.actor_id=actorconte4_.actor_id and actorconte3_.create_date<actorconte4_.create_date)
Actor
,则只需将其添加到where子句中即可。
@Query("select ac from ActorContent ac left outer join fetch ac.actor left outer join fetch ac.meanOfPayment where ac.actor.id = :actorId and ac.id not in (select ac1.id from ActorContent ac1, ActorContent ac2 where ac1.meanOfPayment = ac2.meanOfPayment and ac1.actor = ac2.actor and ac1.createDate < ac2.createDate)")
public List<ActorContent> findLatestForActor(@Param("actorId") Integer actorId);
ActorContent [id=2, actor=Actor [id=2], meanOfPayment=MeanOfPayment [id=1], amount=-10400.00, createDate=2018-10-02 00:00:00.066]
ActorContent [id=3, actor=Actor [id=2], meanOfPayment=MeanOfPayment [id=3], amount=6000.00, createDate=2018-10-02 00:00:00.066]
ActorContent [id=4, actor=Actor [id=2], meanOfPayment=MeanOfPayment [id=2], amount=200.00, createDate=2018-09-30 00:00:00.066]
ActorContent
id字段。您可以使用DTO进行此操作,但JPA无法同时处理投影和
join fetch
,因此您只会得到actor.id和meanOfPayment.id,否则将进行多次选择。在此用例中,多重选择可能不是交易杀手,但您必须自己决定所有这一切。当然,您还可以将
ActorContent
的主键组合为actor.id,meanOfPayment.id和createDate,这将具有上述约束的更多好处。
Entities
。
@Entity
public class Actor {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Entity
public class MeanOfPayment {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Entity
public class ActorContent {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@ManyToOne
private Actor actor;
@ManyToOne
private MeanOfPayment meanOfPayment;
private BigDecimal amount;
@Temporal(TemporalType.TIMESTAMP)
private Date createDate;
关于jpa - 如何在Spring JpaRepository中使用JPQL选择组中的最新记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52744463/
我想写这样的查询:- Select u from bookmaster u order by Year(b.createDate),Month(b.createDate),day(b.createDa
例如:选择不同的新 JPQA.QueryResultObjectV1(space.name,spaceID) "+ "from Space space,Building building,Floor
例如:选择不同的新 JPQA.QueryResultObjectV1(space.name,spaceID) "+ "from Space space,Building building,Floor
我有两张 table 第一张表 - 事件第二张表 - 类别 @Entity public class Event { @Id @GeneratedValue(strategy = Generation
我在我的 DAO 中使用 entityManager。 要创建表格,我使用: String CREATE_TABLE = "CREATE TABLE if not exists cats (name
在 JPQL 中,我可以通过以下方式检索实体: query = entityManager.createQuery("select c from Category c"); List categori
我需要执行以下选择: select c.address from Customer c where lower(trim(c.name)) = :name 但是我得到以下异常: javax.persi
我无法使此查询正常工作: Query query = eManager.createQuery("select c FROM News c WHERE c.NEWSID = :id",News.cla
我在我的 msql 表列中有 Long 类型,该列表示位标记。 我需要选择为其设置了所有几个标志的对象: columnValue & flags = columnValue 我尝试: javax.pe
请查看这篇文章的末尾(找到了另一种但也不起作用的方法)。第二种方式应该是更好的方式,如果有一天它起作用的话;) 我有一个由两个多对多关系组成的实体:MainEntity 有很多 A,MainEntit
首先声明我不是数据库专家。这些东西把我搞糊涂了,但不幸的是我在工作中被迫这样做,所以我有点卡住了:-) 我试图通过在 JPQL 中工作来获得一个复杂的订单,但我一点运气都没有。 我当前的查询如下所示:
我需要将此自联接转换为 JPQL: SELECT s1.* FROM site AS s1 JOIN (SELECT site_type, MAX(last_update_date) AS LastU
如果我有一个扩展 Fruit 的类 Apple,我如何编写一个 JPQL 查询来返回所有严格意义上是 Fruits 而不是 Apples 的对象? 最佳答案 这在 JPA 2.0 中是可能的,使用 T
我正在尝试获取 Order[] 数组,其中包括未收到相关文档的所有订单。 我尝试了这个查询,它返回了正确的行数。 @Query("Select o FROM Order o INNER JOIN o
我正在尝试根据用户可以选择的可能日期列表对日期进行搜索。使用日历,我要求用户输入一个日期,然后我想获取在“data_possibile”列表中具有该日期的所有可能数据包。 这是我正在使用的查询: @N
我想构建一个 JPQL 查询来将该结构的数据映射到这个 DTO: @AllArgsConstructor class UserDTO { long userId; long countOfCo
我必须对表执行批量更新。 做一个快速的例子: UPDATE Book b SET b.amount = b.amount + 1 WHERE b IN ( :books ) 问题是 b.amount
我有以下 JPA实体: @Entity class UserClient{ @Id @GeneratedValue(strategy = GenerationType.AUTO)
我有一个实体(例如 Setting),其中包含集合(例如 Settings.languages)和另一个对象(Language 的实例)。 有没有办法选择其集合实体中包含所需属性值的所有实体?例如获取
我正在使用 spring data jpa:过滤父对象的子对象的最佳方法是什么?在我下面的例子中我想要具有事件子对象的父对象,也只想要事件子对象作为父对象的列表 @Query(select dis
我是一名优秀的程序员,十分优秀!