0

是否可以从查询中查询 Embeddable 对象?这是我的实体:

 @Entity
 @Table(name = "A")
 public class UnitParam implements Serializable {
    ...
    @EmbeddedId
    private UnitParamId unitParamId;
    ....
 }

 @Embeddable
 public class UnitParamId implements Serializable {

    @Column(name = "PcID", nullable = false)
    private short pcId;

    @Column(name = "UnitID", nullable = false)
    private short unitId;

    @Column(name = "ParamID", nullable = false)
    private int paramId;
    ...
 }

 @Entity
 @Table(name = "B")
 public class ParameterMapping extends BasicEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns(value = {
        @JoinColumn(name = "PcID", referencedColumnName = "PcID"),
        @JoinColumn(name = "UnitID", referencedColumnName = "UnitID"),
        @JoinColumn(name = "ParamID", referencedColumnName = "ParamID") })
    private UnitParam unitParam;
 ...
 }

这是失败的查询:

  select p.id, p.name as name, 
   p.unitParam.unitParamId.pcId as processCell,
   p.unitParam.unitParamId.unitId as unit,
   p.unitParam.unitParamId.paramId as paramId 
   from ParameterMapping p


异常:由: org.hibernate.QueryException:无法解析属性:unitParamId:ParameterMapping [SELECT p.id,p.name 作为名称,p.unitParam.unitParamId.pcId 作为 processCell,p.unitParam.unitParamId。 unitParam.unitId 作为单位,p.unitParam.unitParamId.paramId 作为 paramId FROM de.koehl.mes.model.ParameterMapping p]


提前谢谢你。

我发现了问题:第一个问题是混合字段/属性访问。修复后,ManyToOne 生成列,但没有外键!但是不知道为什么!!!!

4

1 回答 1

0

中没有unitParam字段UnitParamId,因此路径p.unitParam.unitParamId.unitParam.unitId无效。将您的查询更改为

select p.id, p.name as name, 
       p.unitParam.unitParamId.pcId as processCell,
       p.unitParam.unitParamId.unitId as unit,
       p.unitParam.unitParamId.paramId as paramId 
from ParameterMapping p

甚至更好:

select p.id, p.name as name, 
       unitParam.unitParamId.pcId as processCell,
       unitParam.unitParamId.unitId as unit,
       unitParam.unitParamId.paramId as paramId 
from ParameterMapping p
inner join p.unitParam unitParam
于 2012-07-06T14:18:09.700 回答