如何连接多个queryDSL表

2024-03-05

我有一些表,我想使用 queryDSL 连接获取结果,但没有找到任何使用 queryDSL 进行多个连接的示例。

我有这些表:

  • 账户表:accountId(PK)|电子邮件 |密码

  • account_profile 表:accountId (PK)(fk 到帐户) |昵称

  • 社区表:articleId (PK) | accountId(fk 到帐户)|标题 |内容

现在我想要下面的 JPQL 成为 queryDSL 代码

select r from community r join r.account.profile a where a.nickname = :nickname

我有实体元模型 - QAccount、QAccountProfile、QCommunity

此外,我必须通过分页获取结果,因此应该使用以下命令调用查询pageable object.

这是我的工作,还没有工作。

JPAQuery</*What generic type expected?*/> query = new JPAQuery</*???*/>(entityManager);
Predicate predicate = query.from(QCommunity.community).join(/*join directly accountProfile? or account? is it QEntity or real entity?*/);

// where should I place nickname matching condition ?


...

list = (repository.findAll(predicate, pageable)).getContent();

昵称匹配条件应该放在哪里?

编辑:附加实体信息

账户.java

@Entity
@Table(name="account", uniqueConstraints={
    @UniqueConstraint(columnNames="account_seq"),
    @UniqueConstraint(columnNames="email")
})
@DynamicInsert
@DynamicUpdate
@Data
@EqualsAndHashCode
@ToString(includeFieldNames=true)
@RequiredArgsConstructor(staticName="of")
@NoArgsConstructor
public class Account implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="account_seq", nullable=false, unique=true)
    private Integer accountId;

    @Column(name="email", nullable=false, unique=true)
    @NonNull
    private String email;

    @NonNull
    private String password;

    @OneToOne(cascade=CascadeType.ALL, mappedBy="account")
    private AccountProfile profile;

    @OneToOne(cascade=CascadeType.ALL, mappedBy="account")
    private AccountSecurity security;
}

账户配置文件.java

@Entity
@Table(name="account_profile", uniqueConstraints={
    @UniqueConstraint(columnNames={"account_seq"}),
    @UniqueConstraint(columnNames={"nickname"})
})
@DynamicInsert
@DynamicUpdate
@Data
@EqualsAndHashCode
@ToString(includeFieldNames=true)
@RequiredArgsConstructor(staticName="of")
@NoArgsConstructor
public class AccountProfile implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="account_seq", referencedColumnName="account_seq")
    private Account account;

    @Column(name="nickname", nullable=false)
    @NonNull
    private String nickname;

}

社区.java

@Entity
@Table(name="community", uniqueConstraints = {
        @UniqueConstraint(columnNames="article_seq")
})
@DynamicInsert
@DynamicUpdate
@Data
@NoArgsConstructor
@EqualsAndHashCode
@ToString(includeFieldNames=true)
public class Community {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="article_seq", nullable=false, unique=true)
    private Long articleId;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="account_seq", referencedColumnName="account_seq")
    private Account account;

    @Column(name="title", nullable=false)
    private String title;

    @Column(name="content", nullable=false)
    private String content;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="reg_dt")
    private Date date;

    @Column(name="read_cnt", nullable=false)
    private int readCount;

    @Column(name="attach_url")
    private String attachUrl;

    @Column(name="attach_filename")
    private String attachFileName;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="article")
    private Set<CommunityReply> replies;
}

编辑:问题已解决

为了帮助像我一样面临问题的其他人,我将发布我的工作代码。该代码正在搜索具有匹配特定昵称的任何社区文章。

@PersistenceContext
    private EntityManager entityManager;


    private List<Community> getList(int pageNo, String keyword, int rowsOnPage){

        int offset = (pageNo -1) * rowsOnPage;
        int limit = rowsOnPage;

        JPAQuery<Community> query = new JPAQuery<Community>(entityManager);

        QCommunity qCommunity = QCommunity.community;
        QAccount qAccount = QAccount.account;
        QAccountProfile qAccountProfile = QAccountProfile.accountProfile;

        return query
            .from(qCommunity)
            .innerJoin(qCommunity.account ,qAccount)
            .innerJoin(qAccount.profile, qAccountProfile)
            .where(qAccountProfile.nickname.like("%"+keyword+"%"))
            .orderBy(qCommunity.articleId.desc())
            .offset(offset)
            .limit(limit)
        .fetch();
    }

首先,为 QueryDSL 查询声明一个自定义扩展基础存储库类。

首先是界面:

@NoRepositoryBean
public interface ExtendedQueryDslJpaRepository<T, ID extends Serializable> 
        extends JpaRepository<T, ID>, QueryDslPredicateExecutor<T> {

    <T1> Page<T1> findAll(JPQLQuery jpqlQuery, Pageable pageable);
}

然后是实现:

public class ExtendedQueryDslJpaRepositoryImpl<T, ID extends Serializable>
        extends QueryDslJpaRepository<T, ID> implements ExtendedQueryDslJpaRepository<T, ID> {

    private static final EntityPathResolver DEFAULT_ENTITY_PATH_RESOLVER = SimpleEntityPathResolver.INSTANCE;

    private final EntityPath<T> path;
    private final PathBuilder<T> builder;
    private final Querydsl querydsl;

    private EntityManager entityManager;

    public ExtendedQueryDslJpaRepositoryImpl(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager) {
        this(entityInformation, entityManager, DEFAULT_ENTITY_PATH_RESOLVER);
    }

    public ExtendedQueryDslJpaRepositoryImpl(JpaEntityInformation<T, ID> entityInformation, 
           EntityManager entityManager, EntityPathResolver resolver) {

        super(entityInformation, entityManager);
        this.path = resolver.createPath(entityInformation.getJavaType());
        this.builder = new PathBuilder(this.path.getType(), this.path.getMetadata());
        this.querydsl = new Querydsl(entityManager, this.builder);
        this.entityManager = entityManager;
    }

    @Override
    public <T1> Page<T1> findAll(JPQLQuery jpqlQuery, Pageable pageable) {

        // Count query
        final JPQLQuery<?> countQuery = jpqlQuery;

        // Apply pagination
        JPQLQuery<T1> query = querydsl.applyPagination(pageable, jpqlQuery);

        // Run query
        return PageableExecutionUtils.getPage(query.fetch(), pageable, countQuery::fetchCount);
    }
}

将新类定义为基类和存储库的基类@Configuration class.

@Configuration
@EnableJpaRepositories(basePackageClasses = ..., repositoryBaseClass = ExtendedQueryDslJpaRepositoryImpl.class)

然后,您的存储库应该从新接口扩展(当然它扩展了 JpaRepository):

@Repository
public interface CommunityRepository extends ExtendedQueryDslJpaRepository<Community, Long> {
}

然后,您可以尝试以下代码:

String nickname = "nick";

QAccount account = QAccount.account;
QAccountProfile accountProfile = QAccountProfile.accountProfile;
QCommunity community = QCommunity.community;

JPQLQuery query = new JPAQuery(entityManager);

BooleanBuilder predicate = new BooleanBuilder();
predicate.and(accountProfile.nickname.eq(nickname));

// select r from community r join r.account.profile a where a.nickname = :nickname
query.from(community)
     .join(community.account, account)
     .join(account.accountProfile, accountProfile)
     .where(predicate);

repository.findAll(query, pageable);

希望有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何连接多个queryDSL表 的相关文章

随机推荐

  • 设置输入高度为父级的 100%

    我在设置输入 键入文本 高度以适合 100 的父母时遇到了一些问题 td 高度 我什至尝试迭代每个输入并使用 jQuery 手动设置它的高度 但这需要相当多的时间 我正在处理的网站有很多单元格 并且仍然无法在 IE 7 和 8 上工作 我有
  • 将表单身份验证添加到 ASP.Net 项目会导致 401.2 未经授权?

    我正在尝试将表单身份验证插入到最初使用 VS 2013 和 ASP Net 4 0 使用无身份验证模板创建的 ASP Net 项目中 我已遵循 MSDN 上的建议 并将其添加到 system web 下的 Web Config 中
  • 添加资源文件到xcode

    我正在尝试将一些新的资源文件添加到由另一个人在另一台 Mac 上构建的项目中 我认为该项目有前人的规定 使用右键单击 gt 将文件添加到 MyProject 不会提供预期的结果 编译项目后 添加的文件在应用程序中不可见 如何在我的项目中添加
  • 最新的 Jersey 示例不起作用

    我已经安装了最新版本的球衣 捆绑版本 2 13 0 以及该版本的示例 然后我尝试了 用于测试 Restful 服务 examples helloworld pure jax rs src main java org glassfish je
  • JavaScript 加载图像的进度

    JS 有没有办法在加载图像时获取加载图像的进度 我想使用HTML5新的Progress标签来显示加载图像的进度 我希望有这样的东西 var someImage new Image someImage onloadprogress funct
  • MongoDB - 大量 MongoCleaner 线程

    不知何故 我的 java 应用程序与 mongodb 通信最终产生了大量名为 MongoCleanerXXX 的停放 睡眠 线程 我认为它来自驱动程序 其数量约为 600 显然数据库存在一些连接问题 在 mongod 重新启动一段时间后确实
  • 我的 httpd.conf 是空的

    我最近在 ubuntu 上安装了 apache2 但我有一个问题 我的 httpd conf 是空的 有人能给我一份 ubuntu 上 apache2 的 httpd conf 的干净副本吗 谢谢 编辑 我看到了你的答案 但在 wampse
  • 如何让dput删除多余的数据?

    我想要一个 SO 问题的最小可重现代码 我一直在使用dput droplevels head df 50 然而 df大约有 4k 条记录 看起来像dput正在为每个人打印一些东西 我需要在问题中显示两个不同的 df 所以不会让我超过 30
  • 打字稿中具有数据水合/脱水的类

    我想分享 React TS 前端和 Node TS 后端之间的 TS 类或接口 问题是 TS 类型在编译时被剥离 所以当我想将类实例转换为 JSON 时我无法使用它们 我想知道是否有任何解决方案可以在静态文件中描述我的对象 生成 TS 类
  • 在 .Net 2.0 中对 IList 进行排序的最佳方法是什么?

    我有一个IList
  • 当调试器设置为 LLDB 时,Xcode 4 挂起附加到(应用程序名称)

    当我在模拟器中运行应用程序时 Xcode 挂在 附加到 应用程序名称 上 但这仅在调试器设置为 LLDB 时发生 当调试器设置为 GDB 时 应用程序运行良好 产品 gt 编辑方案 gt 运行 gt 调试器 如何修复此问题以使用 LLDB
  • C static 关键字与 C++ 私有作用域?

    本地翻译单元的 C 等效项是什么staticC 中的函数 例如有以下内容bar c static void bar 在C 中 这会被写成私有成员函数吗 class foo void bar void foo bar 私有成员函数隐式引入了t
  • 用例图包括

    我有一个关于用例图的问题 如图所示 用户可以输入或更新他的姓名和问题 正如您所看到的 用户在第一次输入信息时需要输入姓名和问题 因此包括在内 但是 如果他希望更新他的信息 图表是否表明他必须修改名称和问题 因为它们包含在内 例如 如果他拼错
  • Economist.com 如何实施其粘性标题? jQuery?

    如果您访问 经济学人 网站上的一篇文章 例如 http www economist com node 17629757 http www economist com node 17629757 当您向下滚动页面超过某个点 使用 PAGEDO
  • 如何禁用 <> 的自动关闭而不禁用其他括号 () {}?

    我对自动完成的功能感到恼火 lt gt Rust 的 VSCode 中的括号 虽然它在指定泛型类型时可能很有用 但当它为我的小于运算符自动完成 gt 时 它确实让我烦恼 我知道我可以完全禁用自动关闭括号 但是有没有办法指定其中哪些应该被视为
  • 如何使用 matplotlib 在 python 中绘制 3D 密度图

    我有一个 x y z 蛋白质位置的大型数据集 并且想将高占用率区域绘制为热图 理想情况下 输出应该类似于下面的体积可视化 但我不确定如何使用 matplotlib 实现这一点 我最初的想法是将我的位置显示为 3D 散点图 并通过 KDE 对
  • 让 Flex 容器采用内容的宽度,而不是宽度 100%

    在下面的示例中 我有一个具有以下样式的按钮 button flexbox approach other button styles display flex justify content center align items center
  • 如何使用 Windows api 更改时区设置

    我需要在我的应用程序中通过 API 更改 DST 和时区 我在以下链接末尾修改并复制了 GetTimeZoneInformation 用法的示例并运行了它 http msdn microsoft com en us library wind
  • 来自 gstatic 的 PhantomJS JavaScript 错误,但浏览器中没有错误

    我最近通过 PhantomJS 测试套件运行我们的网站 遇到了无法在浏览器中手动重现的 JavaScript 错误 这些错误是在 Google 地图 api 中发现的 Capybara 返回的文本如下 TypeError Unable to
  • 如何连接多个queryDSL表

    我有一些表 我想使用 queryDSL 连接获取结果 但没有找到任何使用 queryDSL 进行多个连接的示例 我有这些表 账户表 accountId PK 电子邮件 密码 account profile 表 accountId PK fk