如何返回标准的结果鲜明 [英] how to return criteria Distinct Result

查看:195
本文介绍了如何返回标准的结果鲜明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有比尔和一对多和多对一的关系Bill_Details。我需要得到比尔列表帮助。

I have Bill and Bill_Details with onetomany and manytoone Relationship. I need a help in getting Bill List.

POJO的

比尔

@OneToMany(cascade={CascadeType.ALL},fetch = FetchType.EAGER)
@JoinColumn(name = "bill_id")
private Set<BillDetails> billDetails = new HashSet<BillDetails>();

BillDetails

BillDetails

@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinColumn(name = "bill_id")
private Bill billId;

我使用的预测采取从列表比尔价值。

I am using Projections to take Bill value from List.

DaoHibernate

@Transactional
public List<Bill> getbillDetailsByBillId(String billId) {
    Criteria cr = null;
    try {
        cr = getSession().createCriteria(Bill.class,"bill")
                .createAlias("bill.billDetails","billDetails")
                .setProjection(Projections.projectionList()
  // I tried    .setProjectionProjections.distinct(Projections.projectionList()
                        .add(Projections.property("billNo"),"billNo")
                        .add(Projections.property("billDetails.amount"),"billDetails.amount")
                        .add(Projections.property("billDetails.rate"),"billDetails.rate"))                          
                        .add(Restrictions.eq("id", billId))
                        .setResultTransformer(new AliasToBeanNestedResultTransformer(Bill.class));

    } catch (Exception e) {
        System.out.println("Get bill DetailsByBillId Error----------"+e);
        e.printStackTrace();
    }
    System.out.println(cr.list().size());
    return cr.list();
}

注意:

- >比尔表包含单列

-- > Bill table contains single row

- > BillDetails表包含四列本BILLID

-- > BillDetails table contains four row for this BillId

我的条件查询返回四个对象,而不是单个对象。我也试图与不同的功能。

My criteria Query Returns four Objects instead of single Object. I also tried with distinct feature.

预期输出:

我需要的是包含BillDetails对象(4个值)单个对象。
ie.I低于

I need single object that contains BillDetails Objects(4 Values). ie.I explained with sample Json format below

{billNo:231,
    billDetails[{amount:100,rate:1}{amount:200,rate:2}
     {amount:300,rate:30}{amount:400,rate:4}] }

如何由Hibernate条件查询得到这个?请帮助

How to get this by Hibernate criteria Query ? Please Help

推荐答案

首先,你的映射是不正确的。你有一个双向关联,和侧(一侧)中的一个必须是一个边的逆

First of all, your mapping isn't correct. You have a bidirectional association, and one of the side (the one side) must be the inverse of the one side:

@OneToMany(mappedBy = "billId", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<BillDetails> billDetails = new HashSet<BillDetails>();

您也应该重命名因为它包含的内容是一项法案,和现场BILLID出货,而不是一个法案ID。

You should also rename the field billId to bill, given that what it contains is a bill, and not a bill ID.

现在,您的查询的问题是,您使用的是没有任何理由的预测。当使用预测,你特意选择返回包含单个列行。而且,由于SQL查询返回4行,你回来4账单:每个一行。

Now, the problem with your query is that you're using projections for no reason. When using projections, you deliberately choose to return rows containing individual columns. And since the SQL query returns 4 rows you get back 4 bills: one for each row.

你还让您的生活使用Criteria查询,而不是HQL,这是这种简单的静态查询更适合不必要的复杂。

You're also making your life unnecessarily complicated by using a Criteria query instead of HQL, which is much more suited for such simple static queries.

但即使是HQL查询是无用的在这里,因为你只是想从它的ID的法案。所有你需要的是

But even a HQL query is useless here, since you simply want to get a bill from its ID. All you need is

Bill bill = (Bill) session.get(Bill.class, billId);

这将得到该法案,既然你选择了把一对多协会评为跃跃欲试,它也会立即加载其法案的细节。

This will get the bill, and since you chose to make the OneToMany association as EAGER, it will also immediately load its bill details.

如果您还没有作出协会EAGER(你真的应该把它作为懒惰),你可以使用这个简单的HQL查询,用来加载它的细节一笔账:

If you hadn't made the association EAGER (and you should really leave it as LAZY), you could use this simple HQL query to load a bill with its details:

select distinct b from Bill bill 
left join fetch bill.billDetails 
where bill.id = :billId

这篇关于如何返回标准的结果鲜明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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