无法建立通过“包含在元素集合关系中”关联实体的HQL查询

问题描述

| 我的模型中有以下类/映射:
@Entity
public class UpSaleReason {
    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne
    private Subject subject;

    @ElementCollection
    private Set<Reason> relatesToRegisteredReasons;
}

@Embeddable
public class Reason {
    @ManyToOne
    private Subject subject;

    @Enumerated(value = EnumType.STRING)
    private Category category;
}

@Entity
public class Subject {
    @Id
    private Long id;
    private String name;
}


@Entity
public class ConversationCase {
    @Id
    private Long id;

    @Embedded
    private Reason reason;
}
并尝试执行该HQL:
select r from UpSaleReason as r,ConversationCase as cc
where cc.reason in elements(relatedReasons) and cc.id = :id
这给了我:   ...引起的:java.sql.sqlException:   语句[选择中的IN谓词中需要单列选择   upsalereas0_.id为id8_,upsalereas0_.subject_id为subject2_8_   来自UpSaleReason upsalereas0_交叉加入ConversationCase conversati1_   其中(conversati1_.subject_id在(选择relatedtor2_.category,   up_sale_to_registered_reasons中的relatedtor2_.subject_id   relatedtor2_,其中up​​salereas0_.id = relatestor2_.UpSaleReason_id))   和conversati1_.id =?] 我应该怎么做才能将HQL中的两个实体通过可以被描述为“一个实体的组件属性值应包含在另一个实体的ElementCollection中”的关系相互关联?     

解决方法

问题出在
in elements(relatedReasons)
中,它为类别和主题均完整的原因对象生成内部选择语句。 看起来应该是
elements(relatedReasons.subject)