Select phrases ordered by frequency

@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;
​
}

Could you tell me what should I do for cluster.getPhrases() to select phrases ordered by frequency?

>Solution :

try

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

see here
How do you order a oneToMany join table in hibernate criteria

Leave a Reply