0

Given the following I am trying to force the child collection (countryData) to be loaded when I perform the query, this works however I end up with duplicates of the Bin records loaded.

public Collection<Bin> getBinsByPromotion(String season, String promotion) {
    final Session session = sessionFactory.getCurrentSession();
    try {
        session.beginTransaction();
        return (List<Bin>) session.createCriteria(Bin.class).
                setFetchMode("countryData", FetchMode.JOIN).
                add(Restrictions.eq("key.seasonCode", season)).
                add(Restrictions.eq("key.promotionCode", promotion)).
                add(Restrictions.ne("status", "closed")).
                list();
    } finally {
        session.getTransaction().commit();
    }
}

I don't want the default (lazy) behavior as the query will return ~8k records thus sending 16k additional queries off to get the child records.

If nothing else I'd prefer.

select ... from bins b where b.seasonCode = ?
                         and b.promotionCode = ?
                         and b.status <> 'Closed';
select ... from binCountry bc where bc.seasonCode = ?
                                and bc.promotionCode = ?;

1 Answer 1

3

you can use CriteriaSpecification.DISTINCT_ROOT_ENTITY;

criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
Sign up to request clarification or add additional context in comments.

1 Comment

Seems to solve the problem, though it causes the server to take a lot longer than I should, possibly a vendor specific problem (using Progress OpenEdge RDBMS). I've reverted to not mapping the collection and retrieving it separately via two queries which perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.