Criteria 查询whith FROM 子句,仅包含一个查询,不包含任何表

发布于 2024-09-08 00:33:56 字数 3446 浏览 8 评论 0原文

我有以下查询,我不知道如何使用条件编写它。

SELECT questions_cnt, COUNT(*) users_cnt
FROM (
    SELECT COUNT(DISTINCT question) questions_cnt
    FROM UserAnswer
    GROUP BY user
) t
GROUP BY questions_cnt

我已经开始编写条件查询。看起来像下面

final DetachedCriteria subCriteria = DetachedCriteria.forClass(UserAnswer.class)
    .setProjection(
        Projections.projectionList()
            .add(Projections.alias(Projections.countDistinct("question"), "questions_cnt"))
            .add(Projections.groupProperty("user"))
    );
final DetachedCriteria criteria = DetachedCriteria.forClass(???);

我已经成功编写了子查询,但我不知道如何编写主查询。 DetachedCriteria 需要创建实体类,但我的主查询不使用任何表。它使用 from 子句中的另一个查询作为其源。

UserAnswer 条目具有以下结构

@Entity
@Table(name = "user_answer")
public final class UserAnswer extends AbstractEntity {

    private static final long serialVersionUID = -3149418434619291049L;

    private Long id;
    private Date timestamp = new Date();
    private Answer answer;
    private Question question;
    private String userProvidedAnswer;
    private CourseSession courseSession;
    private User user;
    private Boolean checked;

    @Id
    @Override
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    @Version
    @Column(name = "timestamp", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    @ManyToOne
    @ForeignKey(name = "fk__user_answer__answer")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JoinColumn(name = "answer_id")
    public Answer getAnswer() {
        return answer;
    }

    public void setAnswer(Answer answer) {
        this.answer = answer;
    }

    @ManyToOne
    @ForeignKey(name = "fk__user_answer__question")
    // @OnDelete(action = OnDeleteAction.CASCADE) // SQL Server does not support a cascade operation for this column
    @JoinColumn(name = "question_id", nullable = false)
    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    @Column(name = "user_provided_answer", length = 255)
    public String getUserProvidedAnswer() {
        return userProvidedAnswer;
    }

    public void setUserProvidedAnswer(String userProvidedAnswer) {
        this.userProvidedAnswer = StringUtils.trimToNull(userProvidedAnswer);
    }

    @ManyToOne
    @ForeignKey(name = "fk__user_answer__course_session")
    @JoinColumn(name = "course_session_id", nullable = false)
    public CourseSession getCourseSession() {
        return courseSession;
    }

    public void setCourseSession(CourseSession courseSession) {
        this.courseSession = courseSession;
    }

    @ManyToOne
    @ForeignKey(name = "fk__user_answer__user")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JoinColumn(name = "user_id", nullable = false)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Column(name = "is_checked")
    public Boolean getChecked() {
        return checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }
}

I have the following query and I don't know how can I write it using criteria.

SELECT questions_cnt, COUNT(*) users_cnt
FROM (
    SELECT COUNT(DISTINCT question) questions_cnt
    FROM UserAnswer
    GROUP BY user
) t
GROUP BY questions_cnt

I have started to write criteria query. It looks like the following

final DetachedCriteria subCriteria = DetachedCriteria.forClass(UserAnswer.class)
    .setProjection(
        Projections.projectionList()
            .add(Projections.alias(Projections.countDistinct("question"), "questions_cnt"))
            .add(Projections.groupProperty("user"))
    );
final DetachedCriteria criteria = DetachedCriteria.forClass(???);

I have succesfully written subquery, but I don't know how main query can be written. DetachedCriteria requires entity class to be created, but my main query does not use any tables. It uses another query in from clause as its source.

UserAnswer entiry has the following structure

@Entity
@Table(name = "user_answer")
public final class UserAnswer extends AbstractEntity {

    private static final long serialVersionUID = -3149418434619291049L;

    private Long id;
    private Date timestamp = new Date();
    private Answer answer;
    private Question question;
    private String userProvidedAnswer;
    private CourseSession courseSession;
    private User user;
    private Boolean checked;

    @Id
    @Override
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    @Version
    @Column(name = "timestamp", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    @ManyToOne
    @ForeignKey(name = "fk__user_answer__answer")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JoinColumn(name = "answer_id")
    public Answer getAnswer() {
        return answer;
    }

    public void setAnswer(Answer answer) {
        this.answer = answer;
    }

    @ManyToOne
    @ForeignKey(name = "fk__user_answer__question")
    // @OnDelete(action = OnDeleteAction.CASCADE) // SQL Server does not support a cascade operation for this column
    @JoinColumn(name = "question_id", nullable = false)
    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    @Column(name = "user_provided_answer", length = 255)
    public String getUserProvidedAnswer() {
        return userProvidedAnswer;
    }

    public void setUserProvidedAnswer(String userProvidedAnswer) {
        this.userProvidedAnswer = StringUtils.trimToNull(userProvidedAnswer);
    }

    @ManyToOne
    @ForeignKey(name = "fk__user_answer__course_session")
    @JoinColumn(name = "course_session_id", nullable = false)
    public CourseSession getCourseSession() {
        return courseSession;
    }

    public void setCourseSession(CourseSession courseSession) {
        this.courseSession = courseSession;
    }

    @ManyToOne
    @ForeignKey(name = "fk__user_answer__user")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JoinColumn(name = "user_id", nullable = false)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Column(name = "is_checked")
    public Boolean getChecked() {
        return checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文