2

我正在使用 Java 中的休眠条件进行 MySql 查询。我在下面给出我的代码的小快照。

        Criteria criteria = getCurrentSession().createCriteria(A.class, "a");
        ProjectionList projections = Projections.projectionList();

        projections.add(Projections.distinct(Projections.property("a.column_of_a")));
        projections.add(Projections.property("b.column_of_b"));
        projections.add(Projections.property("c.column_of_c"));
        criteria.setProjection(projections);

        criteria.createAlias("a.listName1", "b", JoinType.LEFT_OUTER_JOIN);
        criteria.createAlias("b.listName2", "c", JoinType.LEFT_OUTER_JOIN);
        criteria.createAlias("c.listName3", "d", JoinType.LEFT_OUTER_JOIN);

        criteria.add(Restrictions.eq("a.column_of_a", someValue));
        criteria.add(Restrictions.eq("c.column_of_c", someValue));

        criteria.addOrder(Order.asc("a.column_of_a"));
        criteria.addOrder(Order.desc("b.column_of_b"));

        @SuppressWarnings("unchecked")
        List<Object[]> records = criteria.list();

        for(int i=0;i<3;i++)
        {
            Object record[] = records.get(i);
            Object value = record[1];
            System.out.println("value::"+value);
        }

现在我的问题是,当我在 Java 代码中获得结果并在 Eclipse 控制台中打印时,每次执行代码时我都会以不同的顺序获得结果。但是当我将 MySql 查询从控制台复制并粘贴到 MySql 编辑器时,我每次都会得到相同的结果。当然在 mySql 编辑器中必须给出相同的结果。但不明白为什么休眠标准每次都会以不同的顺序给我结果?

提前致谢。每个答案将不胜感激。

4

1 回答 1

1
    Criteria criteria = getCurrentSession().createCriteria(A.class, "a");


    criteria.createAlias("a.listName1", "b", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("b.listName2", "c", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("c.listName3", "d", JoinType.LEFT_OUTER_JOIN);

    ProjectionList projections = Projections.projectionList();

    projections.add(Projections.distinct(Projections.property("a.column_of_a")));
    projections.add(Projections.property("b.column_of_b"));
    projections.add(Projections.property("c.column_of_c"));
    criteria.setProjection(projections);

    criteria.add(Restrictions.eq("a.column_of_a", someValue));
    criteria.add(Restrictions.eq("c.column_of_c", someValue));

    criteria.addOrder(Order.asc("a.column_of_a"));
    criteria.addOrder(Order.desc("b.column_of_b"));

    @SuppressWarnings("unchecked")
    List<Object[]> records = criteria.list();

    for(int i=0;i<3;i++)
    {
        Object record[] = records.get(i);
        Object value = record[1];
        System.out.println("value::"+value);
    }

我认为您的代码在创建别名之前正在投影属性。试试上面的代码...

于 2013-08-05T17:17:14.277 回答