我试图了解如何从hibernate获取列表 - 但我不断收到错误,例如无法直接在组件上创建对象。虽然我已经找到了解决这个问题的方法,但是我发现了创建别名的参考资料 - 这似乎在我的情况下失败了。具体来说,我试图在复合键的一个元素上查询对象。
鉴于以下两个类:
@Embeddable
public class CveReferenceId implements java.io.Serializable {
private String refsource;
private String refname;
private String cveid;
@Column(name = "refsource", nullable = false)
public String getRefsource() {return this.refsource;}
public void setRefsource(String refsource) {this.refsource = refsource;}
@Column(name = "refname", nullable = false)
public String getRefname() {return this.refname;}
public void setRefname(String refname) {this.refname = refname;}
@Column(name = "cveid", nullable = false)
public String getCveid() {return this.cveid;}
public void setCveid(String cveid) {this.cveid = cveid;}
}
@Entity
@Table(name = "cve_reference")
@Immutable
public class CveReference implements java.io.Serializable {
private CveReferenceId id;
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "refsource", column = @Column(name = "refsource", nullable = false)),
@AttributeOverride(name = "refname", column = @Column(name = "refname", nullable = false)),
@AttributeOverride(name = "cveid", column = @Column(name = "cveid", nullable = false))})
public CveReferenceId getId() {return this.id;}
public void setId(CveReferenceId id) {this.id = id;}
}
当我尝试执行以下代码来检索CVE引用列表时,我得到'不是关联:id.cveid':
Session session = HibernateUtil.getSession();
Criteria criteria = session.createCriteria(CveReference.class)
.createAlias("id.cveid", "refid")
.add(Restrictions.eq("refid", "CVE-2013-1950"));
List<CveReference> refs = criteria.list();
当我运行以下代码时,我得到'不能直接在组件上创建Criteria对象。在拥有实体上创建条件并使用点属性访问组件属性':
Session session = HibernateUtil.getSession();
Criteria criteria = session.createCriteria(CveReference.class)
.createAlias("id", "ref")
.add(Restrictions.eq("ref.id", "CVE-2013-1950"));
List<CveReference> refs = criteria.list();
对于我在这里缺少的任何提示将不胜感激。
- 杰里米
答案 0 :(得分:1)
我找到了问题的答案。显然,生成的注释不正确,我需要使用@IdClass。以下作品:
@Entity
@Table(name = "cve_reference")
@IdClass(CveReferenceId.class)
@Immutable
public class CveReference implements java.io.Serializable {
private String refsource;
private String refname;
private String cveid;
public CveReference() {
}
public CveReference(String refsource, String refname, String cveid) {
this.refsource = refsource;
this.refname = refname;
this.cveid = cveid;
}
@Id
@Column(name = "refsource", nullable = false)
public String getRefsource() {
return this.refsource;
}
public void setRefsource(String refsource) {
this.refsource = refsource;
}
@Id
@Column(name = "refname", nullable = false)
public String getRefname() {
return this.refname;
}
public void setRefname(String refname) {
this.refname = refname;
}
@Id
@Column(name = "cveid", nullable = false)
public String getCveid() {
return this.cveid;
}
public void setCveid(String cveid) {
this.cveid = cveid;
}
}
public class CveReferenceId implements java.io.Serializable {
private String refsource;
private String refname;
private String cveid;
public CveReferenceId() {
}
public CveReferenceId(String refsource, String refname, String cveid) {
this.refsource = refsource;
this.refname = refname;
this.cveid = cveid;
}
public String getRefsource() {
return this.refsource;
}
public void setRefsource(String refsource) {
this.refsource = refsource;
}
public String getRefname() {
return this.refname;
}
public void setRefname(String refname) {
this.refname = refname;
}
public String getCveid() {
return this.cveid;
}
public void setCveid(String cveid) {
this.cveid = cveid;
}
}
然后按如下方式创建标准:
Session session = HibernateUtil.getSession();
Criteria criteria = session.createCriteria(CveReference.class)
.add(Restrictions.eq("cveid", "CVE-2013-1950"));
List<CveReference> refs = criteria.list();