gpt4 book ai didi

java - 查询仅在子实体不为空时返回实体

转载 作者:行者123 更新时间:2023-12-01 19:19:34 24 4
gpt4 key购买 nike

我有以下查询:

  @Query("SELECT new de.hiqs.project.Project(project.id, project.name, project.isArchived, " +
"project.customer.id, project.customer.name, project.customer.smallestTimeUnitEnabled, " +
"project.customer.smallestTimeUnit, project.defaultDailyEntrySettings.id, " +
"project.defaultDailyEntrySettings.performanceRecord, project.defaultDailyEntrySettings.startDateHour," +
"project.defaultDailyEntrySettings.startDateMinute, project.defaultDailyEntrySettings.endDateHour, " +
"project.defaultDailyEntrySettings.endDateMinute, project.defaultDailyEntrySettings.pause ) " +
"FROM Project project")
List<Project> findAllInOneRequest();

所以我有一个查询,它返回具有所选属性的所有项目,以便在一个请求中获取所有内容并避免 N+1 问题。如您所见,ProjectDefaultDailyEntrySettings 存在关系(外键)。问题是并非每个项目都有 DefaultDailyEntrySettings 子项(数据库中为 null)。因此查询仅返回有一个的projects。我希望它返回所有 projects 并在 defaultDailyEntrySettings 不存在时将其初始化为 null,而不是在没有 defaultdailyEntrySettings 的情况下不返回 projects 。有办法做到吗?

另一种选择是自动为每个project提供一个带有空字段的defaultDailyEntrySettings,而不是外键为空,但我不知道如何使用注释来做到这一点。

最佳答案

也许是这样的?

@Query("SELECT new de.hiqs.project.Project(project.id, project.name, project.isArchived, " +
"project.customer.id, project.customer.name, project.customer.smallestTimeUnitEnabled, " +
"project.customer.smallestTimeUnit, project.defaultDailyEntrySettings.id, " +
"project.defaultDailyEntrySettings.performanceRecord, project.defaultDailyEntrySettings.startDateHour," +
"project.defaultDailyEntrySettings.startDateMinute, project.defaultDailyEntrySettings.endDateHour, " +
"project.defaultDailyEntrySettings.endDateMinute, project.defaultDailyEntrySettings.pause ) " +
"FROM Project project " +
"WHERE EXISTS (SELECT 1 FROM project.defaultDailyEntrySettings)" +
"UNION ALL " +
"SELECT new de.hiqs.project.Project(project.id, project.name, project.isArchived, " +
"project.customer.id, project.customer.name, project.customer.smallestTimeUnitEnabled, " +
"project.customer.smallestTimeUnit, null, " +
"null, null," +
"null, null, " +
"null, null ) " +
"FROM Project project " +
"WHERE NOT EXISTS (SELECT 1 FROM project.defaultDailyEntrySettings)")
List<Project> findAllInOneRequest();

关于java - 查询仅在子实体不为空时返回实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59374948/

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