您好,我正在尝试在标准中引用组合键的属性,该组合键在实体上定义为和@Embeddable
@Entity
@Table(name = "B_J_P")
public class BJP implements java.io.Serializable {
private BJPId id;
private BJI bJI;
public BJP() {
}
public BJP(BJPId id, BJI bJI) {
this.id = id;
this.bJI = bJI;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "jIId", column = @Column(name = "J_I_ID", nullable = false)),
@AttributeOverride(name = "kN", column = @Column(name = "K_N", nullable = false, length = 100)),
public BJPId getId() {
return this.id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "J_I_ID", nullable = false, insertable = false, updatable = false)
public BJI getBJI() {
return this.bJI;
}
}
我需要通过以下方式到达kName:
@Embeddable
public class BJPId implements java.io.Serializable {
private long jIId;
private String kName;
public BJPId() {
}
public BJPId(long jIId, String kN) {
this.jIId = jIId;
this.kN = kN;
}
@Column(name = "J_I_ID", nullable = false)
public long getJIId() {
return this.jIId;
}
@Column(name = "K_NAME", nullable = false, length = 100)
public String getKName() {
return this.kName;
}
}
但是当我尝试从BJP是具有以下条件的属性的基类中获取它时
DetachedCriteria timestampFilter = DetachedCriteria.forClass(BJP.class)
.createAlias("id","alias")
.add(Restrictions.eq("alias.kName","DataSetName"))
.setProjection(Projections.property("kName"));
我收到以下错误:
org.hibernate.QueryException: Criteria objects cannot be created directly on components. Create a criteria on owning entity and use a dotted property to access component property: id
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathInfo
我应该如何制定条件查询,以便到达kName属性,以便在动态sql上下文中基于该条件应用过滤?
如果我没有提供足够的相关信息,请询问我忘记提供完整背景信息的地方。
编辑:根据Genzetto的建议,我设法达到了元素(至少现在没有给出错误),但是一旦执行此操作,则不会返回任何结果:
DetachedCriteria timestampFilter = DetachedCriteria.forClass(BJP.class)
.add(Restrictions.eq("id.kName","DataSetName"))
.setProjection(Projections.property("id.kName"));
Session currentSession = sessionFactory.getCurrentSession();
Criteria query = currentSession.createCriteria(BJI.class)
.add(Subqueries.propertyEq("bJP",timestampFilter))
就像在查看SQL时的格式一样
... where this_.J_INST_ID = (select this_.K_NAME as y0_ from .B_J_P this_ where this_.K_NAME=?)
它试图将子查询添加到根对象的ID中,尽管我希望它成为bJP的一部分。如何将其添加到正确的位置?
最佳答案
您无需使用别名即可执行此操作。您可以直接访问组合键属性:
DetachedCriteria timestampFilter = DetachedCriteria.forClass(BJP.class)
.add(Restrictions.eq("id.kName","DataSetName"))
.setProjection(Projections.property("id.kName"));