2012-07-06 59 views
0

是否可以從查詢中查詢Embeddable對象?這裏是我的實體:hql和@EmbeddedId

@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 [選擇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會生成列,但不會有外鍵!但我不知道爲什麼!

回答

0

UnitParamId中沒有unitParam字段,所以路徑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 
+0

噢,對不起,這是一個錯字:p.unitParam.unitParamId.unitParam.unitId。我的查詢看起來像你的第一個建議,它不起作用。 – ruediger 2012-07-09 06:30:24

+0

我發現ManyToOne創建varbinary列而不是三列:PcId,UnitId和ParamId。所以,查詢不能工作!哦,看到混合領域/財產訪問。 – ruediger 2012-07-09 11:26:28