views:

67

answers:

1
@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;
    }

Lets say i have Languages Eng Germ, People who speaks Eng, people who speaks German, and people who speaks Eng and German I want to get all of the people who speaks both English and German using Criteria.

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=?
    )
A: 

From within a DAO object that has access a session object (perhaps the one that extends HibernateDaoSupport):

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();
Ice
this will list all the people which speaks Eng, and all the people which speaks Ger, and also all the people speaking both languages, what i need is a criteria that will select only the later, people which speaks both English and German.
DartesMartes
Ahh.. than you need and instead of OR : criteria.add(Restrictions.and(languageEN, languageDE));
Ice
that does not work either, ill get the sql
DartesMartes
it should work... Its a simple query.Here's another variant:Criteria criteria = getSession().createCriteria(Person.class);criteria = criteria.createCriteria("languages");criteria.add(Restrictions.eq("name", "en"));criteria.add(Restrictions.eq("name", "de"));List<Person> result = criteria.list();
Ice
and another:Criteria criteria = getSession().createCriteria(Person.class);criteria = criteria.createCriteria("languages");criteria.add(Restrictions.like("name", "en", MatchMode.EXACT);criteria.add(Restrictions.like("name", "de", MatchMode.EXACT);List<Person> result = criteria.list();
Ice
Logically it should work but it does not...i have put the generated sql query, and used it on the db. And i really have no idea why it does not. I have entered in link_person_languages these values pid: 5, 5, 6 lid 1 2 2
DartesMartes