2009-07-07 63 views
5

比方說,我有這個實體(休眠):Hibernate:查詢包含CollectionOfElements中指定元素的實體嗎?

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

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

例如,我想人的情況下,他們的手機包含「555-1234」。 我該如何對此進行查詢?我在尋找類似的東西:

session.createCriteria(Person.class)./*something*/.add(Restrictions./*something*/"555-1234").list(); 

回答

9

嗨,你可以試試這個

String phone = "555-1234"; 
Person person= (Person) session.createQuery("from Person p join p.phones pl where pl = :phone").setString("phone", phone).uniqueResult(); 
-1

我想你想Hibernate的Restrictions.in()方法,它需要一個屬性名作爲第一個參數,並且或者對象作爲第二個數組或收藏。

參見:The Javadoc

編輯:,在重讀你的問題,我想你可以使用任何的相關Restrictions方法,特別是eq

session.createCriteria(Person.class).add(Restrictions.eq("phones", "555-1234")).list(); 
+0

任何人都測試過它? – whiskeysierra 2010-01-10 13:53:10

相關問題