hql和@EmbeddedId

时间:2012-07-06 12:39:48

标签: jpa

是否可以从查询中查询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 as name,p.unitParam.unitParamId.pcId as processCell,p.unitParam.unitParamId.unitParam.unitId as unit, p.unitParam.unitParamId.paramId as paramId FROM de.koehl.mes.model.ParameterMapping p]


先感谢您。

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

1 个答案:

答案 0 :(得分: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