Tôi có hai thực thể:Làm thế nào để "đặt hàng bằng chức năng tổng hợp" trong dữ liệu mùa xuân?
ResourceFile:
@Entity
@Table(name = "resource_file")
public class ResourceFile extends IdEntity<Integer> {
@Id
@SequenceGenerator(name = "resource_file_id_generator", sequenceName = "resource_file_id", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "resource_file_id_generator")
@Column(name = "id", unique = true, nullable = false)
@Nonnegative
private Integer id;
...
}
FavoriteResourceFile:
@Entity
@Table(name = "favorite_resource_file")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class FavoriteResourceFile extends IdEntity<FavoriteResourceFileId> {
@EmbeddedId
private FavoriteResourceFileId id;
@MapsId("resourceFileId")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "resource_file_id", nullable = false)
private ResourceFile resourceFile;
...
}
Và tôi muốn thực hiện các truy vấn sau "chọn tất cả các file tài nguyên và sắp xếp chúng theo số lượng yêu thích tập tin tài nguyên của ".
Trong SQL nó trông giống như:
select rf.id, count(frf.resource_file_id) from resource_file rf
left join favorite_resource_file frf on frf.resource_file_id = rf.id
group by rf.id
order by count(rf.id) desc;
Nhưng tôi không thể hiểu được làm thế nào để làm điều đó với dữ liệu mùa xuân và làm thế nào để làm cho bản đồ để ResourceFile thực ở cuối.
Một số hạn chế:
- tôi không thể làm cho mối quan hệ với FavoriteResourceFile trong ResourceFile, vì họ đang nằm trong module khác nhau
- Tôi không muốn sử dụng SQL mẹ đẻ hoặc JPA truy vấn (như dây đàn).
- Sẽ thích hợp hơn khi sử dụng Mô hình, Đặc điểm kỹ thuật hoặc QueryDSL, vì chúng đã được sử dụng trong dự án.
Ai đó có thể giúp tôi không?