Hibernate深度嵌套集合限制无效

时间:2018-09-04 08:29:19

标签: java hibernate jpa hql

我的问卷的数据结构相当庞大且混乱,导致HQL较长

select s from Session s join s.questionnaire q join q.categories c join c.questions q2 join q2.answers a where a.session.id=:id2 and s.id=:id

我认为此HQL看起来合理,并且生成的sql也是如此

select session0_.id as id1_9_, session0_.name as name2_9_, session0_.position as position3_9_, session0_.questionnaire as question4_9_ from session session0_ inner join questionnaire questionna1_ on session0_.questionnaire=questionna1_.id inner join category categories2_ on questionna1_.id=categories2_.parent_questionnaire inner join question questions3_ on categories2_.id=questions3_.parent_category inner join answer answers4_ on questions3_.id=answers4_.question where answers4_.session=? and session0_.id=?

...即使它很大,

但是由于某些原因,它完全忽略了answer限制。 在调试器中签入时,我可以看到4个不同会话的4个不同答案。 这怎么可能?

修改

好,这真的很有趣...

select s from Session s join s.questionnaire q join q.categories c where c.name=:name and s.id=:id

这也不起作用,类别也没有过滤,所以我假设“会话”和“问卷”之间的映射是错误的吗?

@Entity
@Table(name = "session")
@Cacheable
@org.hibernate.annotations.Cache(region = "session", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Session implements Serializable {
    @Id
    @Column(name = "id")
    private String id;
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinColumn(name = "questionnaire")
    @LazyCollection(LazyCollectionOption.FALSE)
    private Questionnaire questionnaire;
}

@Entity
@Table(name = "questionnaire")
@Cacheable
@org.hibernate.annotations.Cache(region = "questionnaire", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Questionnaire implements Serializable {
    @Id
    @Column(name = "id")
    private String id;
}

...更改为无效

select q from Questionnaire q join q.categories c where c.name=:name

所以我完全迷路了

0 个答案:

没有答案