gpt4 book ai didi

java - 使用条件和 sqlProjection 连接表

转载 作者:行者123 更新时间:2023-12-01 15:56:26 26 4
gpt4 key购买 nike

我有以下函数来构建 Hibernate Criteria 以生成分箱数据:

private Criteria getCustomBinResultCriteriaSQL(double binSizeX, double binSizeY, String xVar, String yVar, String customBinQuery) {
return createCriteria(Residue.class)
.setProjection(Projections.projectionList()
.add(Projections.sqlGroupProjection(
"floor(" + xVar + " / " + binSizeX + ") * " + binSizeX + " as xBin, " +
"floor(" + yVar + " / " + binSizeY + ") * " + binSizeY + " as yBin, " +
"CAST (" + customBinQuery + " AS DOUBLE PRECISION) as customBinResult",
"xBin, yBin",
new String[] { "xBin", "yBin", "customBinResult" },
new Type[] { Hibernate.DOUBLE, Hibernate.DOUBLE, Hibernate.DOUBLE })))
.setResultTransformer(Transformers.aliasToBean(CustomBinResult.class));
}

对于同一个表(残差)中的数据来说,这一切都非常有效,但假设我的数据结构是这样的:

pdbentry:idpdbvarexpmethod:idexpvarresidue:idresvar

pdbentry has a one-to-one relation with expmethod, and a one-to-many relation with residue.

How would I go about joining the residue table with expmethod, based on the criteria-builder above. So in other words: what do I need to add to the criteria to be able to have "expvar" as xVar?

I tried adding something like:

.setFetchMode("pdbentry", FetchMode.JOIN);
.setFetchMode("expmethod", FetchMode.JOIN);

最后,但我仍然无法将“expvar”或“expmethod.expvar”作为xVar。

有什么想法吗?

最佳答案

我通过别名连接表

.createCriteria("rootEntity.foreignEntity", "someAlias")

这使得 rootEntity.foreignEntity.someProperty 可作为 someAlias.someProperty 供 Criteria 查询使用(通常我选择“someAlias”与“foreignEntity”相同)。

由于我通常需要急切获取,因此我倾向于在 createCriteria 调用中使用 Criteria.INNER_JOINCriteria.LEFT_JOIN

关于java - 使用条件和 sqlProjection 连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4971262/

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