我想询问一下,是否可以创建多个级别深度的查询投影和条件? 我有两个模型类:
@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
*/
}
我的 Criteria 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岁的人?
谢谢