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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:29:39+00:00 2026-05-13T07:29:39+00:00

Lets say we have this JPA-annotated class, with a property of type List. This

  • 0

Lets say we have this JPA-annotated class, with a property of type List. This code is currently working fine.

@Entity
public class Family {
    ...
    @CollectionOfElements(targetElement=java.lang.String.class)
    @JoinTable(name = "elements_family",
        joinColumns = @JoinColumn(name = "idFamily")
    )
    @Column(name = "element", nullable = false)
    private List<String> elements;
    ...
}

Is there any way to query for the list of Families that contain the element “yyy” ? That is, something like:

Query query = getEntityManager().createQuery("select f FROM Family f WHERE element = :element");
query.setParameter("element", "yyy");
return query.getResultList();
  • 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-13T07:29:39+00:00Added an answer on May 13, 2026 at 7:29 am

    Note that you are using Hibernate specific feature CollectionOfElements. This is not strictly JPA. You can HQL specific elements function to do this:

    select f from Family f WHERE :element in elements(f.elements)
    

    Here is a working example:

    import org.hibernate.annotations.CollectionOfElements;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.EntityTransaction;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.Persistence;
    import javax.persistence.Query;
    import java.util.Arrays;
    import java.util.List;
    
        @Entity
        public class Family {
            @Id
            int id;
    
            String name;
    
            @CollectionOfElements(targetElement = java.lang.String.class)
            @JoinTable(name = "elements_family",
                    joinColumns = @JoinColumn(name = "idFamily")
            )
            @Column(name = "element", nullable = false)
            public List<String> elements;
    
            public static void main(String[] args) throws Exception {
                EntityManagerFactory emf = 
                    Persistence.createEntityManagerFactory("mysql");
                EntityManager em = emf.createEntityManager();
                try {
    
                    Family f1 = new Family();
                    f1.id = 1;
                    f1.name = "Happy";
    
                    f1.elements = Arrays.asList("foo", "bar", "yyy", "zzz");
    
                    Family f2 = new Family();
                    f1.id = 2;
                    f2.name = "Disfunctional";
                    f2.elements = Arrays.asList("foo", "bar", "yyy", "xxx");
    
                    EntityTransaction tx = em.getTransaction();
                    tx.begin();
                    em.persist(f1);
                    em.persist(f2);
                    tx.commit();
                    Query query = em.createQuery(
                       "select f from Family f WHERE :element in elements(f.elements)");
                    query.setParameter("element", "bar");
                    List list = query.getResultList();
                    assert list.size() == 2;
    
                } finally {
                    em.close();
                    emf.close();
                }
            }
        }
    

    For the sake of completeness, persistence.xml

    <persistence-unit name="mysql" transaction-type="RESOURCE_LOCAL">
        <class>jpa.Family</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.driver_class" 
                              value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/test"/>
            <property name="hibernate.connection.username" value="test"/>
            <property name="hibernate.connection.password" value="test"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.max_fetch_depth" value="3"/>
            <property name="hibernate.bytecode.use_reflection_optimizer" value="true"/>
            <property name="hibernate.archive.autodetection" value="true"/>
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.generate_statistics" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
        </properties>
    </persistence-unit>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have this code: <?php class hello { var $greeting = hello;
Lets say I have this code: public void MyMethod(string Data, List<string> InputData) { //I
Lets say I have this: <object class=MyClass type=text/html data=/Whatever/1?renderpartial=1></object> <object class=MyClass type=text/html data=/Whatever/2?renderpartial=1></object> And,
Lets say i have this javascript code in an html pages <script type=text/javascript> $(document).ready(function(){
Lets say we have this code: <div id='m1'> <div class=c></div> <div class=c></div> <div class=c></div>
Lets say I have this code: <div class=user> <img class=profilepic src=someimage.jps> <div class=cleaner></div> [
Lets say i have this entity public class Address : Entity { public Address()
Lets say we have this code: class test_t { void* data; public: template <typename
Lets say i have this usercontrol public class test : UserControl { public int

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.