gpt4 book ai didi

java - Hibernate 查询异常

转载 作者:太空宇宙 更新时间:2023-11-04 14:47:02 26 4
gpt4 key购买 nike

您好,我正在尝试在 Criteria 中引用复合键的属性,该属性在实体上定义为 @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"));

关于java - Hibernate 查询异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24281732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com