Hi,
I have OneToMany relationship entities: Client, Doc (documents);
Parent:
@Entity
@Table(name = "CLIENT")
public class Client{
public String getName() {...};
@SuppressWarnings("uncheck
ed")
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "client")
public Set<Doc> getDocs(){...}
}
Child:
@Entity
@Table(name = "DOC")
public class Doc{
public String getCode() {...};
...
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "CLIENT_ID", referencedColumnName = "ID")
public Client getClient(){...}
}
I search Clients by Client.name and/or Doc.code, using Hibernate DetachedCriteria.
public class ClientManagerImpl extends HibernateDaoSupport implements ClientManager {
public List<Client> searchClients(String name, String docCode){
DetachedCriteria criteria = DetachedCriteria.forClass(
Client.cla
ss);
criteria.createAlias("doc"
, "doc");
if (name is not empty){
criteria.add(Restrictions.
ilike("nam
e", name));
}
if (docCode is not empty){
criteria.add(Restrictions.
ilike("doc
.code", docCode));
}
criteria.setResultTransfor
mer(Detach
edCriteria
.DISTINCT_
ROOT_ENTIT
Y);
HibernateTemplate ht = getHibernateTemplate();
ht.setMaxResults(10); // !!!!!!! I want to get a 10 clients max !!!!!! Each users has ~ 5 documents
List<Client> result = ht.findByCriteria(criteria
);
LogUtils.info("Count of Client:" + result.size());
return result;
}
I expected: If search result > 10 clients, searchClients returns list: 10 clients with all documents.
But realy: MaxResults it is number of rows (clients+documents). 2 clients and not all documents.
How do I set max result for Parent objects, and not restrict the number of children objects?
Sorry for my English