How to pass a list in query for a list field of entity.

#1

I have a entity as bellow :-

@Entity
@Table(name = "agent")
public class Agent implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long Id;

    private List<SocialMedia> SocialMedias;

    public Agent()
    {
    }

    public Long getId()
    {
        return Id;
    }

    public List<SocialMedia> getSocialMedias()
    {
        return SocialMedias;
    }

    public void setSocialMedias(List<SocialMedia> SocialMedias)
    {
        this.SocialMedias = SocialMedias;
    }
    
    public enum SocialMedia
    {
        Facebook, Twitter, Instagram, Whatsapp
    }
}

I need all the agents who have active in Facebook and Twitter. what should be the query?

Thanks

#2

I have used "IN" and "Member of" but not working.

code sample :-

    ArrayList<Agent.SocialMedia> soList = new ArrayList<>();
    soList.add(Facebook);
    Query qry = m.em.createQuery(
      "Select a from Agent a where a.SocialMedias in (:so)");
    qry.setParameter("so", soList);
    List<Agent> ents = qry.getResultList();
#3

You may try:

SELECT a from Agent a JOIN a.SocialMedias m where m in :so
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#4

Thank you Its working.


Post Reply

To post a reply and/or subscribe to update notifications - please login