简体   繁体   English

休眠多对多标准投影

[英]Hibernate Many-to-Many Criteria Projection

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 an,m relationship.. And i am using a criteria to do the search - get all the persons which speak ex. 我的情况是这样的,我有两个班级的人和语言,并且有一个,.m关系。.我正在使用一个标准进行搜索-获取所有以前说话的人。 English and German 英文和德文

@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 标准

    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...... 但是,最重要的是,Object []在Set语言应该放置的地方的投影结果是NULL值……

please help tnx 请帮助tnx

What you are doing in very old JDBC style (JDBC is what very old people used to access DB) would be something like this: 您使用非常古老的JDBC样式(JDBC是非常老人们用来访问DB的东西)将是这样的:

SELECT * FROM PERSON WHERE LANGUAGE_ID = 1 AND LANGUAGE_ID = 2

(just example, not exactly SQL) (仅是示例,并不完全是SQL)

And, if you run this sql it will NEVER return a single line (Very sad...) because there is no line in the table with LANGUAGE_ID = 1 AND LANGUAGE_ID = 2. 而且,如果你运行该SQL它永远不会返回一个单行(很伤心......),因为与LANGUAGE_ID = 1 AND LANGUAGE_ID = 2表中没有任何

I don't really know the best way to solve your problem (Hibernate is not my strongest skill), but in your case (if the languages number is not so big) i would make 2 (or 3, or a loop) of selections and join then using a simple Set in code. 我真的不知道解决您的问题的最佳方法(休眠不是我最强的技能),但是在您的情况下(如果语言数量不是那么大),我会选择2个(或3个或一个循环)然后使用简单的Set in代码加入。 Not the best solution... And i will happy if someone show a better way 不是最好的解决方案...如果有人展示出更好的方法,我会很高兴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM