【问题标题】:Getting empty result from the @Query in Hibernate从 Hibernate 中的 @Query 获取空结果
【发布时间】:2020-05-10 12:08:16
【问题描述】:

我正在使用 Hibernate 来执行我的查询,在管理面板中我得到了正确的结果,但是在 Hibernate 中使用时它没有给出任何结果。

道层——

@Query("select new com.eventila.pms.entity.ReferenceLead(projectId,count(lm)) from LeadMaster lm where lm.vendorId= ?1 and lm.source = 'share' group by lm.projectId")
List<ReferenceLead> getReferenceByUser(String userId);

波乔-

@lombok.Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ReferenceLead {

    String projectId;
    Long referenceLead;
    Long count;

    protected ReferenceLead(){}


    public ReferenceLead(String projectId,Long count) {
        this.projectId=projectId;
        this.count=count;
    }

}

执行此操作后,我得到一个空列表。 请帮帮我。

【问题讨论】:

  • 请将您的LeadMaster 实体添加到问题中。

标签: java sql hibernate spring-boot psql


【解决方案1】:

在您的选择查询中返回字段而不调用新的构造函数:

@Query("select projectId, count(lm) from LeadMaster lm where lm.vendorId = ?1 and lm.source = 'share' group by lm.projectId")
List<ReferenceLead> getReferenceByUser(String userId);

Hibernate 将使用这些字段实例化对象。此外,将 @Entity 注释添加到您的 ReferenceLead 类。

【讨论】:

  • 如果我将使用实体注释,那么我必须提及表名对吗?但它只是一个不与任何表关联的 pojo
【解决方案2】:

'source' 是 SQL 中的关键字。

这是 MERGE 中使用的关键字。即当源不匹配时。

单词 MATCHED 也表现出相同的行为,因为它在编辑器中以灰色突出显示。

这些都不是保留关键字,因此如果用作标识符,它们不需要分隔(除非您发现突出显示的语法会分散注意力)。

【讨论】:

  • 你能告诉我解决办法吗?实际上我无法得到你的答案
  • 在您的查询中,您有“lm.source”,它是 SQL 中的保留字。尝试将字段更改为其他字段。
  • 这意味着您可以尝试重命名 LeadMaster 表中的源字段,因为它是 PostgreSQL 和 MySQL 中的保留字。
猜你喜欢
  • 1970-01-01
  • 2016-07-10
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 1970-01-01
相关资源
最近更新 更多