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

HIbernate Criteria with two tables and collection

我有两个表,

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;
}

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 中设置一个标准。但我就是不知道该怎么做?

搜索时,您可以输入客户编号、客户姓名、电话号码 and/or 城市和 select 您要显示的结果数量。

起初我以为我可以写一个命名查询,但后来你有了搜索结果编号和在不同列上对结果进行排序的功能。

我在这里搜索并查看了不同的类似问题,但我就是无法解决问题。 我尝试在按条件列出客户的函数中添加帐户条件,但它不起作用,我不知道这是否与帐户是一个集合有关?

有人能帮帮我吗?

解决方案

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

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

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

之间

我不太清楚你是如何确定哪个账户是银行账户的。 但是,您可能想要这样的东西:

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

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

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

在 .list()

之前