我正在创建一个功能,我的应用程序将显示最喜欢的照片。
我有这门课
public class User{
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.user", cascade = CascadeType.ALL)
private Set<UserLikedPhoto> likedPhotos = new HashSet<UserLikedPhoto>();
//Other properties and accessor methods.
}
照片课
public class Photo{
@OneToMany(fetch = FetchType .LAZY ,mappedBy = "pk.photo")
private Set<UserLikedTrack> likedByUsers = new HashSet<UserLikedTrack>();
//Other properties and accessor methods
}
CompoundId/CompoundObject
@Embeddable
public class UserLikedPhotoId implements Serializable {
@ManyToOne
private UserProfile user;
@ManyToOne
private Photo photo;
//Other Properties and accessor methods.
}
以及包含 CompoundObject 和日期的类
@Entity
@AssociationOverrides({
@AssociationOverride(name = "pk.userId", joinColumns = @JoinColumn(name = "userId")),
@AssociationOverride(name = "pk.photoid", joinColumns = @JoinColumn(name = "photoId")) })
public class UserLikedPhoto{
@EmbeddedId
private UserLikedPhotoId pk = new UserLikedPhotoId();
@Column
@Temporal(TemporalType.DATE)
private Date date;
//Other Entities and accssor methods
}
有了这个类。我会用这种类型的表生成
------------------------------
| date | UserId photoId |
-----------------------------
| 2010-12-23 | 1 | 23 |
| 2010-12-21 | 2 | 23 |
| 2010-12-23 | 1 | 24 |
| 2010-12-21 | 5 | 23 |
现在我想做的是在示例中获得投票最多的照片(可能是给定日期的前 5 或前 10),投票最多的照片是 23 号照片。第二多的投票是 24 号。
在 Hibernate 中,我将如何查询此类任务?