休眠选择所有投影组 [英] hibernate select all projections group by

查看:89
本文介绍了休眠选择所有投影组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在hibernate Criteria规范中,如果我想在表中分组属性,我必须使用Projections.groupProperty。问题是我无法选择表中的所有字段。



我想要的是:

SELECT * FROM实体GROUP BY字段



如果我在hibernate Criteria中使用group,那么groupProperty必须存在,并且sql结果将为:
SELECT字段FROM实体GROUP BY字段。



有什么方法可以获得我想要的吗?我在CriteriaJoinWalker的hibernate代码中看到,当我想选择全部(select *)时,代码已经指定是否有投影。我的假设,如果我想改变行为,我必须改变CriteriaJoinWalker代码。我可以修改代码,也许我可以得到我想要的结果,但如果可以的话,我不希望覆盖我的项目的hibernate核心代码。

  // CriteriaJoinWalker 
if(translator.hasProjection() ))
{
resultTypes = translator.getProjectedTypes();
initProjection(translator.getSelect(),translator.getWhereCondition(),translator.getOrderBy(),translator.getGroupBy(),LockMode.NONE);
} else
{
resultTypes =(new Type [] {
TypeFactory.manyToOne(persister.getEntityName())
});
initAll(translator.getWhereCondition(),translator.getOrderBy(),LockMode.NONE);

谢谢

解决方案

如果您使用Projections,那么您将无法获得整体的实体对象。不幸的是,冬眠不能帮助你。查看此问题,并查看此线程

In hibernate Criteria specification, if I want to group property in table, I must use Projections.groupProperty. The problem is I can't select all the field in the table.

What I want to is:

SELECT * FROM entity GROUP BY field

If i use group in hibernate Criteria, then the groupProperty must be exist, and the sql result will be: SELECT field FROM entity GROUP BY field.

Is there any way to get what I want? I see in hibernate code, in CriteriaJoinWalker, when I want to select all (select *), the code is already specify if there is projection or not. My assumption if I want to change the behavior, I must change the CriteriaJoinWalker code. I can modify the code, and maybe I can get the result what I want, but if I can, I do not want to override the hibernate core code to my project. Is there any way to change it directly without change hibernate code?

    // CriteriaJoinWalker
    if(translator.hasProjection())
    {
        resultTypes = translator.getProjectedTypes();
        initProjection(translator.getSelect(), translator.getWhereCondition(), translator.getOrderBy(), translator.getGroupBy(), LockMode.NONE);
    } else
    {
        resultTypes = (new Type[] {
            TypeFactory.manyToOne(persister.getEntityName())
        });
        initAll(translator.getWhereCondition(), translator.getOrderBy(), LockMode.NONE);
    }

Thanks

解决方案

If you use Projections then you will not be able to get an Entity object as a whole. Unfortunately hibernate cant help you. Check out this Issue and also check out this thread.

这篇关于休眠选择所有投影组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆