gpt4 book ai didi

java - 具有基于注释的复合主键的 Hibernate createCriteria 查询

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:53:35 26 4
gpt4 key购买 nike

在我的项目中,我在编写带有复合主键的 createCriteria 查询时遇到了问题。我的实体类和 DAO 方法如下 -

    @Entity
@Table(name="METRICS")

public class Metrics implements Serializable {

private static final long serialVersionUID = -2580493160757497919L;

@EmbeddedId
protected MetricsID metricsID;

@Column(name="PROJ_PERF")
private String proj_perf;

@Column(name="ANALYSIS")
private String analysis;

public String getProj_perf() {
return proj_perf;
}

public void setProj_perf(String proj_perf) {
this.proj_perf = proj_perf;
}

public String getAnalysis() {
return analysis;
}

public void setAnalysis(String analysis) {
this.analysis = analysis;
}

public MetricsID getMetricsID() {
return metricsID;
}

public void setMetricsID(MetricsID metricsID) {
this.metricsID = metricsID;
}

}

@Embeddable
public class MetricsID implements Serializable {

private static final long serialVersionUID = 4691163770334366543L;

@Column(name="PROJECT_ID")
private String project_id;

@Column(name="METRICS_NO")
private int metrics_no;

public String getProject_id() {
return project_id;
}

public void setProject_id(String project_id) {
this.project_id = project_id;
}

public int getMetrics_n0() {
return metrics_no;
}

public void setMetrics_no(int i) {
this.metrics_no = i;
}
}

@Override
@Transactional
public List<Metrics> viewMetrics(String project_id) throws Exception {
List<Metrics> metrics = (List<Metrics>)sessionFactory.getCurrentSession().
createCriteria(Metrics.class).createAlias("metricsID.project_id", "project_id_alias").
add(Restrictions.eqProperty("project_id_alias.project_id", project_id)).list();
return metrics;
}

我收到的错误是 - org.hibernate.QueryException: not an association: metricsID.project_id

我搜索了几个类似的例子,并根据其中一个搜索结果的建议使用了别名,但这是我第一次使用别名。我究竟做错了什么?

最佳答案

为什么需要使用别名?你试过直接访问吗?

关注此example ,这段代码应该可以工作

@Override
@Transactional
public List<Metrics> viewMetrics(String project_id) throws Exception {
List<Metrics> metrics =
(List<Metrics>) sessionFactory.getCurrentSession()
.createCriteria(Metrics.class)
.add(Restrictions.eq("metricsID.project_id", project_id))
.list();
return metrics;
}

关于java - 具有基于注释的复合主键的 Hibernate createCriteria 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37618457/

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