spring 选择按频率排序的短语

eagi6jfj  于 2023-01-24  发布在  Spring
关注(0)|答案(1)|浏览(109)
@Entity
@Table(name = "clusters", uniqueConstraints = {@UniqueConstraint(name = "unique_cluster_name",
        columnNames = {"clusterName"})})
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ClusterEntity extends BaseEntity {
    @OneToMany(mappedBy = "cluster",
            cascade = CascadeType.REMOVE)
    private List<CorePhraseEntity> phrases = new ArrayList<>();
}
​
​
​
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "phrases", uniqueConstraints = {@UniqueConstraint(name = "unique_phrase",
        columnNames = {"phrase"})})
public class CorePhraseEntity extends BaseEntity implements Phraseable {
​
    private static final String URL_PREFIX = "/semantics/phrases/core/";
    @ManyToOne
    @JoinColumn(name = "cluster_id",
            nullable = false,
            foreignKey = @ForeignKey(name = "cluster_fk"))
    private ClusterEntity cluster;
​
    @NotNull
    @NotEmpty
    @Column(nullable = false,
            columnDefinition = "varchar(1000) default ''")
    private String phrase;
​
    @NotNull
    @Column(nullable = false,
            columnDefinition = "INTEGER DEFAULT -1")
    Integer frequency;
​
}

你能告诉我我应该怎么做才能让cluster.getPhrases()选择按频率排序的短语吗?

bfrts1fy

bfrts1fy1#

尝试

@OneToMany(mappedBy = "cluster", cascade = CascadeType.REMOVE)
@OrderBy("frequency")
private List<CorePhraseEntity> phrases = new ArrayList<>();

请参见此处How do you order a oneToMany join table in hibernate criteria

相关问题