我想问一下,我是否有可能创建超过一层深度的查询预测和标准?我有 2 个模型类:
@Entity
@Table(name = "person")
public class Person implements Serializable {
@Id
@GeneratedValue
private int personID;
private double valueDouble;
private int valueInt;
private String name;
@OneToOne(cascade = {CascadeType.ALL}, orphanRemoval = true)
@JoinColumn(name="wifeId")
private Wife wife;
/*
* Setter Getter
*/
}
@Entity
@Table(name = "wife")
public class Wife implements Serializable {
@Id
@GeneratedValue
@Column(name="wifeId")
private int id;
@Column(name="name")
private String name;
@Column(name="age")
private int age;
/*
* Setter Getter
*/
}
我的标准 API:
ProjectionList projections = Projections.projectionList();
projections.add(Projections.property("this.personID"), "personID");
projections.add(Projections.property("this.wife"), "wife");
projections.add(Projections.property("this.wife.name"), "wife.name");
Criteria criteria = null;
criteria = getHandlerSession().createCriteria(Person.class);
criteria.createCriteria("wife", "wife", JoinType.LEFT.ordinal());
criterion = Restrictions.eq("wife.age", 19);
criteria.add(criterion);
criteria.setProjection(projections);
criteria.setResultTransformer(Transformers.aliasToBean(Person.class));
return criteria.list();
我希望,我可以查询 Person,具有指定的妻子财产条件,并指定返回结果集。所以我使用 Projections 来获取指定的返回结果集
我想要返回 personID、name(Person)、name(Wife)。我必须如何使用 API,我更喜欢使用 Hibernate Criteria API。
这一次,我使用上面的代码来获得我的预期结果,但它会抛出异常并显示错误消息:
Exception in thread "main" org.hibernate.QueryException: could not resolve property: wife.name of: maladzan.model.Person
,以及我Restrictions.eq("wife.age", 19);
是否正确获取妻子年龄为 19 岁的人?
谢谢