[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

Hibernate: How do I set MaxResult for Parent objects? OneToMany relationship.

Asked by SemyonG in J2EE Frameworks

Tags: Hibernate MaxResult OneToMany

Hi,

I have OneToMany relationship entities: Client, Doc (documents);
Parent:
@Entity
@Table(name = "CLIENT")
public class Client{

  public String getName() {...};

  @SuppressWarnings("unchecked")
  @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.class);
    criteria.createAlias("doc", "doc");
    if (name is not empty){
      criteria.add(Restrictions.ilike("name", name));
    }
    if (docCode is not empty){
      criteria.add(Restrictions.ilike("doc.code", docCode));
    }
    criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
    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


 
 







 
Loading Advertisement...
 
[+][-]10/27/09 05:35 AM, ID: 25671662Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/27/09 05:58 AM, ID: 25671843Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/27/09 05:59 AM, ID: 25671851Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/27/09 06:11 AM, ID: 25671972Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/27/09 06:18 AM, ID: 25672049Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/27/09 07:54 AM, ID: 25673060Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/27/09 08:27 AM, ID: 25673464Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625