我正在做的一些连接遇到了一个奇怪的问题。简而言之,如果我进行左连接,我会得到正确的行数。如果我进行左连接提取,我会得到比预期更多的行。
我的实体类是(为简洁起见使用伪代码)
public class One {
...
private Set<Two> twos;
}
public class Two {
...
private Set<Three> threes;
}
public class Three {
...
}
在我的关系中,我在一班有四个二,每个二班有九个三
我原以为一班一班有四个二,每班九个三分。
如果我使用,我会得到这个
List<One> res = em.createQuery("select o from One o left join o.twos t left join t.threes where o.id = 322", One.class).getResultList();
List<Two> twos = resultList.get(0).getTwos(); //results in four twos - CORRECT
List<One> res = em.createQuery("select o from One o left join fetch o.twos t left join t.threes where o.id = 322", One.class).getResultList();
List<Two> twos = resultList.get(0).getTwos(); //results in four twos - CORRECT
List<One> res = em.createQuery("select o from One o left join fetch o.twos t left join fetch.threes where o.id = 322", One.class).getResultList();
List<Two> twos = resultList.get(0).getTwos(); //results in thirty size twos - *INCORRECT* (is four * nine)
为什么第二次 fetch 会导致它成倍增加。我的数据库存在性能问题,因此想要获取连接以尝试改进它们。虽然我似乎得到了比预期更多的结果。
我使用 Oracle XE 作为我的数据库,使用 JPA2/hibernate 作为我的 ORM。
谢谢