-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate Many to Many Criteria Projection
PostPosted: Mon Sep 13, 2010 5:41 pm 
Newbie

Joined: Mon Sep 13, 2010 5:34 pm
Posts: 4
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.