Jpa hql和@EmbeddedId
是否可以从查询中查询可嵌入对象?以下是我的实体:Jpa hql和@EmbeddedId,jpa,Jpa,是否可以从查询中查询可嵌入对象?以下是我的实体: @Entity @Table(name = "A") public class UnitParam implements Serializable { ... @EmbeddedId private UnitParamId unitParamId; .... } @Embeddable public class UnitParamId implements Serializable { @C
@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 of:ParameterMapping [选择p.id,p.name作为名称,p.unitParam.unitParamId.pcId作为processCell,p.unitParam.unitParamId.unitParam.unitId作为单元, p、 unitParam.unitParamId.paramId作为de.koehl.mes.model.PARAMETERMAPING p中的paramId]
先谢谢你。
我发现了问题:第一个问题是混合字段/属性访问。修复后,manytone生成列,但没有外键!但我不知道为什么 在
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
哦,对不起,这是一个输入错误:p.unitParam.unitParamId.unitParam.unitId。我的查询看起来像您的第一个建议,但它不起作用。我发现manytone创建了一个varbinary列,而不是三个列:PcId、UnitId和ParamId。因此,查询无法工作!哦,请参见混合字段/属性访问。