gpt4 book ai didi

java - 将 JPQL 转换为 QueryDSL

转载 作者:行者123 更新时间:2023-11-29 06:34:57 24 4
gpt4 key购买 nike

我在将以下方法转换为 QueryDSL 时遇到问题,非常感谢您的反馈!

javax.persistence.TypedQuery<Long> query = em().createQuery(
"SELECT COUNT( DISTINCT b ) " +
"FROM Project a " +
"JOIN a.projectParticipants b " +
"WHERE a.projectType IN :projectTypes " +
"AND a.projectStatus = :projectStatus", Long.class
);
query.setParameter("projectTypes", filter.getProjectTypes());
query.setParameter("projectStatus", ProjectStatus.ACTIVE);
return query.getSingleResult();

项目类:

public class Project {

@ManyToMany
@JoinTable(name = "project_user", inverseJoinColumns = @JoinColumn(name = "user_id"))
private Set<User> projectParticipants = new TreeSet<>();

}

表示项目 projectParticipation 集的 project_user 表:

CREATE TABLE IF NOT EXISTS `project_user` (
`user_id` bigint(20) NOT NULL,
`project_id` bigint(20) NOT NULL,
PRIMARY KEY (`user_id`,`project_id`),
KEY `project_user_project_id` (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

最佳答案

以下应该有效

query.from(a)
.join(a.projectParticipants, b)
.where(a.projectType.in(projectTypes), a.projectStatus.eq(projectStatus))
.singleResult(b.countDistinct());

关于java - 将 JPQL 转换为 QueryDSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25496484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com