java 从 SQLProjection 中引用外部条件查询别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7822662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Referencing outer criteria query aliases from within an SQLProjection
提问by EkcenierK
I am aware that you can use {alias}
to refer to the root entity within an SQLProjection:
我知道您可以使用{alias}
来引用 SQLProjection 中的根实体:
Projections.sqlProjection("MIN({alias}.field) as value", new String[]{"value"}, new Type[]{new LongType()}))
What I am trying to do is to reference an alias for a non-rootentity:
我想要做的是引用非根实体的别名:
Projections.sqlProjection("MIN(i.powerRestarts) as value", new String[]{"value"}, new Type[]{new LongType()}))
where i
is an alias from the outer criteria query. The code above throws an SQL exception saying that i.powerRestarts
cannot be found.
wherei
是来自外部条件查询的别名。上面的代码抛出一个 SQL 异常,说i.powerRestarts
找不到。
Is it possible to refer to a non-root alias from an SQLProjection?
是否可以从 SQLProjection 中引用非根别名?
回答by EkcenierK
Having done some googling, it appears that this this is not possible - Hibernate only allows inclusion of the root entity alias using {alias}
in the SQL string of the SQLProjection
. I did however find this issue regarding the limitationon the Hibernate JIRA pages.
做了一些谷歌搜索后,这似乎是不可能的 - Hibernate 只允许{alias}
在SQLProjection
. 然而,我确实发现了这个关于Hibernate JIRA 页面限制的问题。
Someone has kindly submitted a patch that allows the use of non-root aliases in the SQLProjection
string, through a new RestrictionsExt
class. Using my example from the question:
有人好心地提交了一个补丁,允许SQLProjection
通过一个新RestrictionsExt
类在字符串中使用非根别名。使用我的问题示例:
Projections.sqlProjection("MIN(i.powerRestarts) as value", new String[]{"value"}, new Type[]{new LongType()}))
The alias i
can now be referenced as:
i
现在可以将别名引用为:
RestrictionsExt.sqlProjection("MIN({i}.powerRestarts) as value", "value", new LongType())
I had to modify the static RestrictionsExt.sqlProjection
method to allow specification of the type for the column alias ("value"
) (here defined as LongType
), as the patch didn't allow this and defaulted to StringType
.
我不得不修改静态RestrictionsExt.sqlProjection
方法以允许指定列别名 ( "value"
)(此处定义为LongType
)的类型,因为补丁不允许这样做并且默认为StringType
.
The SQLAliasedProjection class in the patch also requires access to the following private methods in org.hibernate.loader.criteria.CriteriaQueryTranslator
: getOuterQueryTranslator
and getAliasedCriteria
. To get this to work without modifying the Hibernate source, I used reflection:
补丁中的 SQLAliasedProjection 类还需要访问org.hibernate.loader.criteria.CriteriaQueryTranslator
:getOuterQueryTranslator
和 中的以下私有方法getAliasedCriteria
。为了在不修改 Hibernate 源的情况下使其工作,我使用了反射:
cri = ((org.hibernate.loader.criteria.CriteriaQueryTranslator) criteriaQuery).getAliasedCriteria(alias);
was changed to:
改为:
Method m = ((org.hibernate.loader.criteria.CriteriaQueryTranslator) criteriaQuery).getClass().getDeclaredMethod("getAliasedCriteria", String.class);
m.setAccessible(true);
cri = (Criteria) m.invoke(((org.hibernate.loader.criteria.CriteriaQueryTranslator) criteriaQuery), alias);
Hopefully this will help anyone else facing the same problem.
希望这能帮助其他面临同样问题的人。