gpt4 book ai didi

java - 使用 Hibernate 选择最常见的值

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:12 25 4
gpt4 key购买 nike

@Entity
public class car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id_car;
@OneToOne
private repair id_repair;
}

@Entity
public class repair {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id_repair;
@OneToOne
@JoinColumn(name="id_car")
private car id_car;
}

我编写了 sql 查询,这是我想要接收的内容

SELECT id_car,
COUNT(*) AS c
FROM repair
GROUP BY id_car
ORDER BY c DESC
LIMIT 5

如何使用 Hibernate 实现此目的?我想获取“id_car”和出现的次数。

最佳答案

List results = session.createCriteria(Car.class)
.setProjection( Projections.projectionList()
.add( Projections.rowCount(),"rCount")
.add( Projections.groupProperty("id_car") )
)
.addOrder(Order.desc("rCount") )
.setMaxResults(5)
.list();

关于java - 使用 Hibernate 选择最常见的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34226206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com