How to query a property of type Listin JPA

前端 未结 3 1911
渐次进展
渐次进展 2021-02-05 18:21

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

@Entity
public class Family {
    ...
    @Collecti         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 19:02

    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 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

    
        jpa.Family
        
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    
    

提交回复
热议问题