I have two tables,
Table Customer
ID | CustomerNumber | Name | City | PhoneNumber
Table Accounts
ID | CustomerNumber | AccountNumber | Bank
The relationship between the two tables is CustomerNumber
My Customer mapping
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;
}
And the Account mapping
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;
}
Function 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()));
}
}
}
Now I want to search for customers with bank accounts only. And I want to setup a criteria in Hibernate. But I just don't know how to do this?
When searching you could enter customerNumber, name of customer, phonenumber and/or city and select how many results you want to display.
First I thought I could write a named query, but then you have the search result number and the functionality of sorting the result on different columns.
I have search here and looked at the different similar questions but I just can't get it to work. I tried to add a criteria for account in a function for listing customers by criteria but it just doesn't work and I don't know if it has something to do with the account being a collection?
Could anyone help me please?
I added the following in my method listbycriteria
customerCritera.setFetchMode("accounts",FetchMode.JOIN);
Criteria accountCriteria = customerCriteria.createAlias("accounts","accounts");
Between if(Object != null) and resultSize = QueryHelperUtil.countResults(customerCriteria);
I do not exactly know how you determinante what account is a bank account. However, you probably would like to have something like this:
Criteria criteria = getCurrentSession().createCriteria(Customer.class,"cust");
criteria.createAlias("cust.accounts","acc");
criteria.list();
To check if the account has a bank number add:
.add(Restrictions.isNotNull("acc.bank"));
before the .list()