标准与投影相结合

时间:2015-05-03 22:02:47

标签: hibernate criteria nhibernate-criteria

我将创建此SQL查询// Store current state in a simple variable var isZoomedIn = false; $('.button1').click(function(){ if(isZoomedIn) { // Zoom it out $('div .textwidget').first().css({ 'background': 'none', 'height': '100%' }); $('.textwidget img').first().css({ 'transform': 'scale(1)', 'transition': '1s', 'margin-left': '0px' }); $(this).text('VIEW IN A ROOM'); } else { // Zoom it in $('div .textwidget').first().css({ 'background': 'url(http://art.smartcredit.md/wp-content/uploads/2015/04/view_in_room_couch_plant.jpg)', 'background-size': '133%', 'height': '520px' }); $('.textwidget img').first().css({ 'transform': 'scale(0.4)', 'transition': '1s', 'margin-left': '-25px' }); $(this).text('BACK TO ARTWORK'); } }); 等效项:

SQL:

Criteria

这是我的SELECT orders.id, book.title, orderitem.quantity FROM orderitem INNER JOIN book ON book.id = orderitem.book_id INNER JOIN orders ON orders.id = orderitem.orders_id WHERE user_id = 1 查询:

Criteria

它没有例外,但只显示Criteria c = getSession().createCriteria(OrderItem.class, "oi"); c.setProjection(Projections.property("quantity")); c.createAlias("oi.book", "book"); c.setProjection(Projections.property("title")); c.createAlias("oi.orders", "or"); c.setProjection(Projections.property("id")); c.createAlias("or.user", "usr"); c.add(Restrictions.eq("usr.id", "1")); return c.list(); 列:

Orders.id

2 个答案:

答案 0 :(得分:4)

使用ProjectionList。它将返回包含字段quantity,title,id的Object数组。

Criteria c = getSession().createCriteria(OrderItem.class, "oi");
c.createAlias("oi.book", "book");
c.createAlias("oi.orders", "or");
c.createAlias("or.user", "usr");

ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("oi.quantity"));
proList.add(Projections.property("book.title"));
proList.add(Projections.property("or.id"));
c.setProjection(proList);
c.add(Restrictions.eq("usr.id", "1"));
return c.list();

答案 1 :(得分:2)

我需要将结果列表作为命名密钥&索引和&以外的值值。我使用了以下方式。

Criteria c = getSession().createCriteria(OrderItem.class, "oi");
c.createAlias("oi.book", "book");
c.createAlias("oi.orders", "or");
c.createAlias("or.user", "usr");

ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("oi.quantity"), "quantity");
proList.add(Projections.property("book.title"), "title");
proList.add(Projections.property("or.id"), "id");
c.setProjection(proList);

c.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

c.add(Restrictions.eq("usr.id", "1"));
return c.list();