2010-09-14 85 views
3
@Entity 
public class Person implements Serializable { 
    private int id; 
      ........... 
    private Set<Languages> languages = new HashSet<Languages>(); 
     ............... 
    @ManyToMany 
    @JoinTable(name = "link_person_languages") 
    public Set<Languages> getLanguages() { 
     return languages; 
    } 
} 

@Entity 
public class Languages implements Serializable { 
    private int id; 
    private String name; 
    @Id 
    @GeneratedValue 
    public int getId() { 
     return id; 
    } 
    @Column(nullable = false, length = 40, unique = true) 
    public String getName() { 
     return name; 
    } 

可以說我有語言工程胚芽,誰講英人,人誰講德語,和人民誰講英,德 我想所有誰講人皆有之英語和德語使用標準。標準多到很多Hibernate

crit.createAlias("languages", "l"); 
Conjunction con = Restrictions.conjunction(); 
for (int j = 0; j < o.length; j++) { 
      Criterion tmp = Restrictions.eq("l.id", ((Languages)o[j]).getId()); 
     con.add(tmp); 
} 
crit.add(con); 


select 
this_.id as y0_, 
    this_.lastName as y1_, 
    this_.firstName as y2_, 
    this_.socialNumber as y3_ 
from 
    Person this_ 
inner join 
    link_person_languages languages3_ 
     on this_.id=languages3_.Person_id 
inner join 
    Languages l1_ 
     on languages3_.languages_id=l1_.id 
where 
    (
     l1_.id=? 
     and l1_.id=? 
    ) 

回答

0

從能夠訪問會話對象(也許延伸的HibernateDaoSupport的)一個DAO對象中:

Criteria criteria = getSession().createCriteria(Person.class); 
criteria = criteria.createCriteria("languages"); 

Criterion languageEN = Restrictions.eq("name", "en"); 
Criterion languageDE = Restrictions.eq("name", "de"); 
criteria.add(Restrictions.and(languageEN, languageDE)); 

List<Person> result = criteria.list(); 
+0

這將列出所有講工程的人,以及所有講GER人,以及所有講這兩種語言的人,我需要的是一個標準,只選擇後來講英語和德語的人。 – Darwly 2010-09-15 11:36:04

+0

啊..比你需要而不是OR:criteria.add(Restrictions.and(languageEN,languageDE)); – Ice 2010-09-15 11:40:13

+0

即使不工作,生病得到sql – Darwly 2010-09-15 11:47:02