[英]HIbernate Criteria with two tables and collection
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 两个表之间的关系是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 功能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 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.
我想在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. 搜索时,您可以输入customerNumber,客户名称,电话号码和/或城市,然后选择要显示的结果数。
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 我在方法listbycriteria中添加了以下内容
customerCritera.setFetchMode("accounts",FetchMode.JOIN);
Criteria accountCriteria = customerCriteria.createAlias("accounts","accounts");
Between if(Object != null) and resultSize = QueryHelperUtil.countResults(customerCriteria); 在if(Object!= null)和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() 在.list()之前
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.