Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3236244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:35:58+00:00 2026-05-17T17:35:58+00:00

I would like to make a query from an object I have defined that

  • 0

I would like to make a query from an object I have defined that contains a collection. Object looks like this:

@Entity
public class ValidationLog {
    @Embeddable
    public static class ValidationLogPK implements Serializable {
        private String dataKey;
        @Enumerated(EnumType.STRING)
        private DataType dataType;
    }

    @EmbeddedId
    private ValidationLogPK id;

    @Enumerated(EnumType.STRING)
    private ValidationResult validationResult;

    @CollectionOfElements(fetch = FetchType.EAGER)
    @JoinTable
    @Enumerated(EnumType.STRING)
    private Set<ValidationRule> validationRules;
}

And query looks something like this:

"select v.id.dataKey from ValidationLog v " + 
    " where v.validationResult = :result" +
    " and v.id.dataType = :type" +
    " and :rule in indices(v.validationRules)"

However this does not work. The “indices”-function I am unsure of though. The thing is I would like to get all “dataKeys” that is of a specified type, result and rule. The problem is that each “dataKey” can have many rules as you can see… So how do I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:35:59+00:00Added an answer on May 17, 2026 at 5:35 pm

    Are you looking for MEMBER OF? Something like this in JPQL:

    SELECT v.id.dataKey
    FROM ValidationLog v
    WHERE v.validationResult = :result
      AND v.id.dataType = :type
      AND :rule MEMBER OF v.validationRules
    

    If you face HHH-5209 (not sure you will, I reported this issue against Hibernate 3.5), try the HQL variant:

    SELECT v.id.dataKey
    FROM ValidationLog v
    WHERE v.validationResult = :result
      AND v.id.dataType = :type
      AND :rule IN elements(v.validationRules)
    

    Update: There is another issue when using in elements on a collection of enums, namely HHH-5159. The problem is not with the query itself, it’s with the parameter binding. When using:

    query.setParameter("rule", ValidationRule.FOO);
    

    Hibernate binds a serialized version of the enum (in my case, the query just returns nothing). However, using the following worked for me:

    query.setParameter("rule", ValidationRule.FOO.name());
    

    Update #2: I am sorry but I don’t think I’ll be able to help further. What I posted about the in element on a collection of enum works for me when used as suggested with Hibernate 3.4 and HSQLDB. Here is the test:

    @Test
    // http://opensource.atlassian.com/projects/hibernate/browse/HHH-5159
    public void testQueryWithInElementOfCollectionOfElementsOfEnums() {
        Person person = new Person("Bruce", "Wayne");
        Set<SomeEnum> someEnums = new HashSet<SomeEnum>();
        someEnums.add(SomeEnum.ONE);
        someEnums.add(SomeEnum.TWO);
        someEnums.add(SomeEnum.FIVE);
    
        person.setSomeEnums(someEnums);
    
        session.persist(person);
    
        String queryString = "SELECT p FROM Person p WHERE :someEnum in elements(p.someEnums)";
    
        Query query = session.createQuery(queryString);
        // query.setParameter("someEnum", SomeEnum.FIVE); // doesn't work, see HHH-5159
        query.setParameter("someEnum", SomeEnum.FIVE.name());
        List actual = query.list();
        assertNotNull(actual);
        assertEquals(1, actual.size());
    }
    

    And the logs:

    ...
    12:40:06.353 [main] DEBUG org.hibernate.SQL - 
        select
            person0_.id as id11_,
            person0_.dept as dept11_,
            person0_.firstName as firstName11_,
            person0_.gender as gender11_,
            person0_.lastName as lastName11_ 
        from
            Person person0_ 
        where
            ? in (
                select
                    someenums1_.element 
                from
                    Person_someEnums someenums1_ 
                where
                    person0_.id=someenums1_.Person_id
            )
    12:40:06.357 [main] TRACE org.hibernate.type.StringType - binding 'FIVE' to parameter: 1
    12:40:06.359 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
    12:40:06.361 [main] TRACE org.hibernate.type.IntegerType - returning '1' as column: id11_
    12:40:06.363 [main] DEBUG org.hibernate.loader.Loader - result row: EntityKey[com.acme.domain.Person#1]
    

    Maybe try to simplify the query to narrow down the problem. I’m not sure it’s related to the in element part.

    References

    • JPA 1.0 Specification
      • Section 4.6.12 “Collection Member Expressions”
    • Hibernate Core Reference Guide
      • 14.10. Expressions
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.