@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();