0

I have some entities as below

@Entity
public class Contact {
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "contact")
    private Set<ContactDetails> details = new HashSet<>();
}

@Entity
public class ContactDetails {
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    private Contact contact;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "contact_details_values", joinColumn = @JoinColumn(name = "contact_detail_id"))
    @Column(name = "value")
    private Set<String> values = new HashSet<String>();
}

and I have a method that selects range of Contacts with DetachedCriteria as below:

public List<Contact> getContactsByRange(int start, int length) {
    DetachedCriteria criteria = DetachedCriteria.forClass(Contact.class);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    Criteria executableCriteria = criteria.getExecutableCriteria(currentSession);
    executableCriteria.setFirstResult(start);
    executableCriteria.setMaxResults(length);
    return executableCriteria.list();
}

The problem is when I call method with 0 and 10 it will return all contacts in the database, but when I call it with 0 and 1 it will return first contact in database with its first detail value and if I call it with 1 and 1 it will return first contact in database with its second detail value.

SRF
  • 587
  • 5
  • 16
  • I check it again and found that it will apply length on details not contacts, in other worlds it returns contacts until the count of details get to length. How should I tell to criteria to apply limitation on contact not details. – SRF Oct 23 '16 at 13:59

1 Answers1

0

Finally I found the answer. I use following code to my method:

criteria.setFetchMode("details", FetchMode.SELECT);
SRF
  • 587
  • 5
  • 16