无法构建通过'包含在元素集合关系中'关联实体的SQL查询

时间:2011-05-25 14:25:02

标签: hibernate components 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谓词中需要单列选择[select   upsalereas0_.id为id8_,upsalereas0_.subject_id为subject2_8_   来自UpSaleReason upsalereas0_ cross join ConversationCase conversati1_   where(conversati1_.subject_id in(select relatedstor2_.category,   来自up_sale_to_registered_reasons的relatestor2_.subject_id   relatestor2_ where upsalereas0_.id = relatestor2_.UpSaleReason_id))   和conversati1_.id =?]

我应该怎样做才能将HQL中的两个相互关联的关系相互关联起来,这个关系可以被描述为“一个实体的组件属性值应该包含在其他的ElementCollection中”?

1 个答案:

答案 0 :(得分:0)

问题出在in elements(relatedReasons),它为完整的原因对象生成内部select语句,同时包含类别和主题。

看起来应该是elements(relatedReasons.subject)