Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Query entities which contain a specified element in a CollectionOfElements?

Let's say I have this entity (for Hibernate):

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    @CollectionOfElements
    @IndexColumn("phones_index")
    Set<String> phones;
}

For example, I want to get instances of Person where their phones contain "555-1234". How can I do a query on this? I am looking for something similar to:

session.createCriteria(Person.class)./*something*/.add(Restrictions./*something*/"555-1234").list();
like image 558
Randy Sugianto 'Yuku' Avatar asked Jul 07 '09 07:07

Randy Sugianto 'Yuku'


1 Answers

Hi you can try this one

String phone = "555-1234";
Person person= (Person) session.createQuery("from Person p join p.phones pl where pl = :phone").setString("phone", phone).uniqueResult();
like image 50
imambenjol Avatar answered Nov 14 '22 23:11

imambenjol