Hi all,
I am new here so if this topic is not in the right subforum, please move it where it should be.
I have posted my problem on couple forums but no answer till now. So here it goes.
EDIT> i am at a dead end... so i can continue looking for the main reason .. Please tell me how to make a simple criteria for many to many relationships which has more than one eq restrictions, for an example, how to get the person speaking eng & german in the example shown here...
My situation is like this i have two classes person and languages, with a n,m relationship.. And i am using a criteria to do the search - get all the persons which speak ex. English and German
Code:
@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;
}
Criteria
Code:
Criteria crit = session.createCriteria(Person.class);
crit.setCacheable(true);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("languages"));
c = enumMap.get(attr);
if (c.isChanged()) {
Criteria crit2 = crit.createCriteria("languages");
Object[] o = (Object[]) c.getAnswer();
Conjunction con = Restrictions.conjunction();
for (int j = 0; j < o.length; j++) {
Criterion tmp = Restrictions.eq("id", ((Languages)o[j]).getId());
con.add(tmp);
}
crit2.add(con);
}
crit.setProjection(projList);
retList = crit.list();
And the funny thing is, if i set it only for one language, i get the proper list of persons, but for more than one language i get none, i rechecked my base and set one person specifficaly to speak the 2 languages. But what tips mi more than anything is that the result from the projection in the Object[] on the place where the Set languages should be, there is NULL value......
please help tnx