我想问一下,我有可能创建查询投影和多个级别的标准? 我有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();
,我希望,我可以查询人,与妻子属性指定的标准,以及指定的返回结果集。 所以我用投影获得指定的返回结果集
我想personID,名称(人),姓名(妻子)将返回。我如何使用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她的年龄价值的人是否正确?
感谢
嗨萨米安多尼。我确实使用过你的AliasToBeanNestedResultTransformer创建嵌套对象,我把嵌套对象作为嵌套对象,但是我有一个小问题。我打算只在嵌套对象中获得特定字段,在父对象中只有几个字段,但结果是父对象中的所有字段和嵌套对象中的所有字段作为嵌套对象。我不知道你的自定义转换器是否只能获取特定的字段,是否可以只嵌套对象中的特定字段作为嵌套对象? –
你有没有解决这个问题? –
@JatinMalwal有什么问题? –