Hibernate 多对多标准投影

发布于 2024-09-18 19:00:01 字数 1690 浏览 7 评论 0原文

编辑>我陷入了死胡同...所以我可以继续寻找主要原因..请告诉我如何为具有多个 eq 限制的多对多关系制定一个简单的标准,例如,如何获得讲英语的人此处显示的示例中的德语...

我的情况是这样的,我有两类人和语言,具有一个,m关系..并且我正在使用一个标准来进行搜索 - 获取所有说前语的人。英语和德语

@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 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();

有趣的是,如果我仅将其设置为一种语言,我会得到正确的人员列表,但对于不止一种语言,我没有得到任何信息,我重新检查了我的基础并专门设置一个人说 2 种语言语言。但最让我提示的是,在Object[]中投影到Set languages应该在的地方的结果,有NULL值……

请帮忙 tnx

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

@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......

please help
tnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2024-09-25 19:00:08

你在非常古老的 JDBC 风格(JDBC 是非常老的人用来访问数据库的方式)中所做的事情将是这样的:(

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

只是示例,不完全是 SQL)

而且,如果你运行这个 sql,它永远不会返回一行(非常悲伤...)因为表中没有 LANGUAGE_ID = 1 AND LANGUAGE_ID = 2 的

我真的不知道解决您的问题的最佳方法(Hibernate 不是我最强的技能) ),但在你的情况下(如果语言数量不是那么大)我会做出 2 个(或 3 个,或一个循环)选择并加入,然后使用代码中的简单 Set 进行连接。不是最好的解决方案......如果有人展示更好的方法,我会很高兴

What you are doing in very old JDBC style (JDBC is what very old people used to access DB) would be something like this:

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

(just example, not exactly 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.

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. Not the best solution... And i will happy if someone show a better way

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文