当前位置: 首页 > 知识库问答 >
问题:

如何将FROM-子查询转换为Hibernate准则语句

盖玉石
2023-03-14

我有以下三个类的模型。A类包含1个B,也可以包含0个或更多个C。B类和C类都包含我想在A类范围内一起求和的金额。

class TableA {
    @Id
    Id id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "table_b_id", nullable = false)
    TableB tableB;

    @OneToMany(mappedBy = "tableA", fetch = FetchType.LAZY)
    Set<TableC> tableCs;
}

class TableB {
    @Id
    Id id;

    @Column(name = "amount_b")
    Long amountB;
}

class TableC {
    @Id
    Id id;

    @Column(name = "amount_c", nullable = false)
    Long amountC;

    @JoinColumn(name = "table_a_id")
    TableA tableA;
}
select compositeSum from 
(SELECT sum(tableC.amountC) + tableB.amountB as 'compositeSum'
FROM tableA A
left join tableB B on A.b_id = B.id
left join tableC C on C.a_id = A.id) SUBQUERY
where compositeSum between 0 and 100;
Long min = 0l;
Long max = 100l;

DetachedCriteria subquery = DetachedCriteria.forClass(TableA.class, "inner")
                .createAlias("tableC", "tableC", JoinType.LEFT_OUTER_JOIN)
                .createAlias("tableB", "tableB")
                .setProjection(Projections.sqlProjection("coalesce(sum(amountC), 0) + amountB", new String[] {"compositeSum"}, new Type[] {StandardBasicTypes.LONG}));

Criteria criteria = session().createCriteria(TableA.class, "outer")
                .add(Subqueries.ge(max, subquery))
                .add(Subqueries.le(min, subquery));
select * from A this_ 
where ? >= 
(select sum(amountC) + amountB from table_A tableA_ 
inner join table_B tableB2_ on tableA_.table_b_id=tableB2_.id 
inner join table_C tableC1_ on tableA_.id=tableC1_.table_a_id) 
and ? <= 
(select sum(amountC) + amountB from table_A tableA_ 
inner join tableB tableB2_ on tableA_.table_B_id=tableB2_.id 
inner join table_C tableC1_ on tableA_.id=tableC1_.table_a_id)

有人知道在Hibernate条件中是否可以向FROM-子句提供子查询吗?多谢!

(已更新)

显然,在内部查询中添加一个指向外部查询的附加筛选器解决了这个问题。要从子查询中引用父查询实体,请使用神奇的关键字“this”。

Long min = 0l;
Long max = 100l;

DetachedCriteria subquery = DetachedCriteria.forClass(TableA.class, "inner")
                .createAlias("tableC", "tableC", JoinType.LEFT_OUTER_JOIN)
                .createAlias("tableB", "tableB")
                .setProjection(Projections.sqlProjection("coalesce(sum(amountC), 0) + amountB", new String[] {"compositeSum"}, new Type[] {StandardBasicTypes.LONG}))
                .add(Restrictions.eqProperty("id", "this.id"));

Criteria criteria = session().createCriteria(TableA.class, "outer")
                .setProjection(Projections.property("id"))
                .add(Subqueries.ge(max, subquery))
                .add(Subqueries.le(min, subquery));
select * from A this_ 
where ? >= 
(select sum(amountC) + amountB from table_A tableA_ 
inner join table_B tableB2_ on tableA_.table_b_id=tableB2_.id 
inner join table_C tableC1_ on tableA_.id=tableC1_.table_a_id 
where tableA_.id = this_.id) 
and ? <= 
(select sum(amountC) + amountB from table_A tableA_ 
inner join tableB tableB2_ on tableA_.table_B_id=tableB2_.id 
inner join table_C tableC1_ on tableA_.id=tableC1_.table_a_id
where tableA_.id = this_.id)
SELECT coalesce(sum(amountC),0) + amountB as 'compositeSum'
FROM table_A tableA
left join table_B tableB on tableA.table_b_id = tableB.id
left join table_C tableC on tableC.table_a_id = tableA.id

group by m.id
having compositeSum between 0 and 100

共有1个答案

壤驷瑾瑜
2023-03-14

您不需要子查询。如下所示简化查询:

SELECT sum(tableC.amountC) + tableB.amountB as 'compositeSum'
FROM tableA A
left join tableB B on A.b_id = B.id
left join tableC C on C.a_id = A.id
where (sum(tableC.amountC) + tableB.amountB) between 0 and 100;

现在相应地更改Hibernate代码。

 类似资料:
  • 我在Mysql中有一个查询, 我已经完成了简单的查询转换为hibernate查询。我将所有pojo类绑定到表。请指导我如何将多个连接查询转换为一个hibernate查询。

  • 我使用了cakephp Mysql到mongodb查询组件,即将Mysql查询转换到mongodb中,但是当查询有多个括号时代码停止工作,我还尝试将http://www.querymongo.com/site上的查询转换为相同的问题,

  • 我需要将这个sql查询转换为hibernate条件,请大家帮忙。 按名称顺序按应用描述限制3从设备组中选择名称,计数(*)为应用

  • 我想在JPA 2.1中将“命名查询”转换为“SQL查询”,并在运行之前对其进行更改。 例如,我有一个命名查询:从CU所在的客户中选择CU。代码=?1,我希望在PAR之后获得查询并对其进行转换和编辑(例如添加模式名称)并创建此查询:从db1.cc1cust cu中选择*其中cu.cc1cod=?1. 我该怎么做? 问候

  • 2013年,根据@Timo Westkämper(参见QueryDSL-Add subquery into FROM语句),可以在FROM子句中包含子查询。现在看来这已经不可能了,因为JPQL规范不允许这样做: (https://docs.oracle.com/cd/e12839_01/apirefs.1111/e13946/ejb3_langref.html#ejb3_langref_subqu

  • 问题内容: 我想从数据库中删除某些项目。我有以下查询: 这有效,并返回2个结果。 现在,我想将此查询转换为查询。但是,以下操作无效: MySQL引发以下错误: 1064-您的SQL语法有误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“ WHERE entry.sheetID = sheets.id ANDsheets.clientID = 13”附近使用 我在这里做错