具有两个表和集合的Hibernate条件

时间:2018-09-11 07:45:16

标签: java hibernate collections criteria hibernate-criteria

我有两个桌子,

Table Customer
ID  |  CustomerNumber  |  Name  |  City  |  PhoneNumber

Table Accounts
ID  |  CustomerNumber  |  AccountNumber  |  Bank

两个表之间的关系是CustomerNumber

我的客户映射

public class Customer {
    @Id
    @Column(name = "ID")
    private Integer ID;
    @Column(name = "CustomerNumber")
    private Integer customerNumber;
    @Column(name = "Name")
    private String name;
    @Column(name = "City")
    private String city;
    @Column(name = "PhoneNumber")
    private Integer phoneNumber;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "customerNumber")
    private Collection<Account> accounts;
}

和帐户映射

public class Account {
    @Id
    @Column(name = "ID")
    private Integer ID;
    @JoinColumn(name = "CustomerNumber", referencedColumnName="CustomerNumber")
    @ManyToOne(fetch = FetchType.LAZY)
    private Customer customer;
    @Column(name = "AccountNumber")
    private Integer accountNumber;
    @Column(name = "Bank")
    private String bank;
}

功能i DAO

public SearchResult listByCriteria(Customer object, int maxResults, String sortProperty, boolean ascending) {
    SearcResult result = new SearchResult();
    int resultSize = 0;
    Criteria customerCriteria = ((Session)em.getDelegate()).createCriteria(Customer.class);
    if(maxResults > 0) {
        customerCriteria.setMaxResults(maxResults);
    }
    try {
        if(object != null) {
            customerCriteria = QueryHelper.getCustomerCriteria(object, customerCriteria, "");
            resultSize = QueryHelperUtil.countResults(customerCriteria);
            result.setSize(resultSize);
            customerCriteria.setFirstResult(index);
            QueryHelperUtil.sortResults(customerCriteria, sortProperty, ascending);
            result.setList(customerCriteria.list());
        }
    } catch (Exception ex) {
        error(ex);
    }
    return result;
}

QueryHelper.getCustomerCriteria

public static Criteria getCustomerCriteria(Customer object, Criteria criteria, String alias) {
    if (object != null) {
        if (object.getCustomerNumber() != null && object.getCustomerNumber() > 0) {
        criteria.add(Restrictions.eq(alias+"customerNumber", object.getCustomerID()));
        }
        if (!StringUtil.isNullOrEmpty(object.getName())) {
        criteria.add(Restrictions.like(alias+"name", QueryHelperUtil.createLikeStatement(object.getName())));
        }
        if (!StringUtil.isNullOrEmpty(object.getCity())) {
        criteria.add(Restrictions.like(alias+"city", QueryHelperUtil.createLikeStatement(object.getCity())));
        }
        if (object.getPhoneNumber())) {
        criteria.add(Restrictions.eq(alias+"phoneNumber", object.getPhoneNumber()));
        }
    }
}

现在,我只想搜索具有银行帐户的客户。我想在Hibernate中设置一个条件。但是我只是不知道该怎么做?

搜索时,您可以输入customerNumber,客户名称,电话号码和/或城市,并选择要显示的结果数。

首先,我以为我可以编写一个命名查询,但是随后您有了搜索结果编号,并且可以在不同的列上对结果进行排序。

我在这里搜索并查看了类似的不同问题,但我无法使其正常工作。 我试图在按条件列出客户的功能中添加一个帐户条件,但是它不起作用,我不知道这是否与该帐户成为托收人有关?

有人可以帮我吗?

解决方案

我在方法listbycriteria中添加了以下内容

customerCritera.setFetchMode("accounts",FetchMode.JOIN);
Criteria accountCriteria = customerCriteria.createAlias("accounts","accounts");

if(Object!= null) resultSize = QueryHelperUtil.countResults(customerCriteria);

1 个答案:

答案 0 :(得分:0)

我不完全知道您如何确定哪个帐户是银行帐户。 但是,您可能想要这样的东西:

   Criteria criteria = getCurrentSession().createCriteria(Customer.class,"cust");
   criteria.createAlias("cust.accounts","acc");
   criteria.list();

要检查帐户是否具有银行号码,请添加:

.add(Restrictions.isNotNull("acc.bank"));

.list()之前