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 6163053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:45:39+00:00 2026-05-23T21:45:39+00:00

I have an entity ‘Parent’ which has a Set of enums private Set<MyEnum> myEnums

  • 0

I have an entity ‘Parent’ which has a Set of enums

private Set<MyEnum> myEnums = EnumSet.noneOf(MyEnum.class);

    @CollectionOfElements(targetElement=MyEnum.class)
    @JoinTable
        (name="PARENT_MY_ENUM",
        joinColumns=@JoinColumn(name="PARENT_ID"))
    @Enumerated(EnumType.STRING)
    @Column (name="MY_ENUM", nullable=false)
    public Set<MyEnum> getMyEnums(){
        return myEnums;
    }


public MyEnum {
ENUM_A,
ENUM_B, 
ENUM_C,
ENUM_D;
}

Now I want to search for this entity with a collection of MyEnums. Only entities where all enums are set as in the search collection should be returned.
So if entity A has ENUM_A, ENUM_B and ENUM_C and entity B has ENUM_B, ENUM_C, ENUM_D a search with the search collectoin ENUM_A, ENUM_B, ENUM_C should only return entity A. A search for ENUM_B and ENUM_C should return nothing.

How would I do that in Hibernate?
if I do

select p from Parent p where p.myEnums IN (:searchCollection) and size(p.myEnums) = size(:searchCollection)

then this would return both entities for the first search.

Any ideas?

Update: I got a step further by figuring out how to do it in MySQL but applying this to Hibernate generates invalid SQL.
You would use a subquery with EXISTS for it like:

WHERE EXISTS(
    SELECT pa.PARENT_ID, count(pme.MY_ENUM) FROM PARENT pa, PARENT_MY_ENUM pme
    where pa.PARENT_ID = pme.PARENT_ID 
    AND pme.MY_ENUM IN ('ENUM_A','ENUM_B')
    GROUP BY pa.PARENT_ID HAVING count(pme.MY_ENUM) = 2
)

But when I try to do the same in Hibernate:

select pa.ParentId, count(pa.myEnums) from Parent pa 
WHERE pa.myEnums IN ('ENUM_A','ENUM_B')
GROUP BY pa.ParentId HAVING count(pa.myEnums) = 2

Hiberante creates this SQL statement:

select pa.CONTAINER_RELEASE_REFERENCE_ID as col_0_0_, count(.) as col_1_0_ from PARENT pa, PARENT_MY_ENUM enum1, PARENT_MY_ENUM enum2, PARENT_MY_ENUM enum3 
where pa.PARENT_ID=enum1.PARENT_ID and pa.PARENT_ID=enum2.PARENT_ID and pa.PARENT_ID=enum3.PARENT_ID 
and (. in ('ENUM_A' , 'ENUM_B')) 
group by pa.PARENT_ID having count(.)=2

MySQL complains about the ‘.’, Where is that coming from and why is Hibernate using 3 joins to PARENT_MY_ENUM?

Is this a Hibernate bug or what am I doing wrong?

  • 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-23T21:45:39+00:00Added an answer on May 23, 2026 at 9:45 pm

    Give the following a try for you exists subselect

    select pa.ParentId, count(en) from Parent pa join pa.myEnums as en
    WHERE en IN ('ENUM_A','ENUM_B')
    GROUP BY pa.ParentId HAVING count(en) = 2
    

    Otherwise, I wonder if something like that might not do the job

    select p from Parent p join p.myEnums em
    where (:results) = elements(em)
    

    or

    select p from Parent p join p.myEnums em
    where (:results) in elements(em)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my business model I have Entity class (IPoint interface ) that has a
I have entity called Customer and it has three properties: public class Customer {
suppose i have @Entity A, which has an eager @ManyToOne to @Entity B, and
I have an entity class: Cabbage.java package womble; public class Cabbage { private int
I have entity objects: @Entity public class Tag { @Id private Long id; @Transient
I have Entity set and i create partial class [Bind(Include = ID,Note,Spec,Retired)] [MetadataType(typeof(SomeClass))] public
I have entity A which has an IList of B called Bs and B
I have a entity which has a list of Child1 objects. The mappings seem
I have entity Ideas, which has child entity collection ChildIdeas. I need to load
I have entity class which is used to model a weather station, and it

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.