我正在使用Java中的hibernate标准进行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编辑器中必须给出相同的结果。但是没有理解为什么hibernate标准每次都以不同的顺序给我结果?
提前致谢。每个答案都会受到赞赏。
答案 0 :(得分: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);
}
我认为您的代码在创建别名之前会投射属性。尝试上面的代码...