5

我想问一下,我是否有可能创建超过一层深度的查询预测和标准?我有 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 岁的人?

谢谢

4

4 回答 4

6

AFAIK 不可能用别名变压器投射超过一层的深度。你的选择是

  • 创建一个扁平化的数据传输对象 (DTO)
  • 自己将生成的 Person 填充到内存中
  • 实现您自己的结果转换器(类似于选项 2)

选项 1 如下所示:

Criteria criteria = getHandlerSession().createCriteria(Person.class)
    .createAlias("wife", "wife", JoinType.LEFT.ordinal())
    .add(Restrictions.eq("wife.age", 19)); 
    .setProjection(Projections.projectionList()
        .add(Projections.property("personID"), "personID")
        .add(Projections.property("name"), "personName")
        .add(Projections.property("wife.name"), "wifeName"));
    .setResultTransformer(Transformers.aliasToBean(PersonWifeDto.class));

return criteria.list();
于 2012-08-29T06:30:20.713 回答
5

我写的ResultTransformer,就是这样做的。它的名字是,在githubAliasToBeanNestedResultTransformer上查看。

于 2013-09-11T01:05:03.083 回答
1

谢谢萨米安多尼。我能够使用您的 AliasToBeanNestedResultTransformer 稍作修改以适应我的情况。我发现嵌套转换器不支持字段位于超类中的场景,因此我对其进行了增强,以在您要投影到的类的类继承层次结构中查找最多 10 级的字段:

    public Object transformTuple(Object[] tuple, String[] aliases) {

        ...


                if (alias.contains(".")) {
                    nestedAliases.add(alias);

                    String[] sp = alias.split("\\.");
                    String fieldName = sp[0];
                    String aliasName = sp[1];

                    Class<?> subclass = getDeclaredFieldForClassOrSuperClasses(resultClass, fieldName, 1);
...
}

其中 getDeclaredFieldForClassOrSuperClasses() 定义如下:

private Class<?> getDeclaredFieldForClassOrSuperClasses(Class<?> resultClass, String fieldName, int level) throws NoSuchFieldException{
    Class<?> result = null;
    try {
        result = resultClass.getDeclaredField(fieldName).getType();
    } catch (NoSuchFieldException e) {
        if (level <= 10){
        return getDeclaredFieldForClassOrSuperClasses(
                resultClass.getSuperclass(), fieldName, level++);
        } else {
            throw e;
        }
    }
    return result;
}

我对这个嵌套属性的 Hibernate 投影如下所示:

Projections.projectionList().add( Property.forName("metadata.copyright").as("productMetadata.copyright"));

我投影的课程如下所示:

public class ProductMetadata extends AbstractMetadata {
...
}

public abstract class AbstractMetadata {
...   
   protected String copyright;
...
}
于 2015-02-02T06:27:45.377 回答
-1

Data Transfer Object (DTO)
而不是在下面创建projectionlist更改,它将为您工作。

    ProjectionList projections = Projections.projectionList(); 
    projections.add(Projections.property("person.personID"), "personID");
    projections.add(Projections.property("person.wife"), "wife");
    projections.add(Projections.property("wife.name"));

    Criteria criteria = null; 
    criteria = getHandlerSession().createCriteria(Person.class,"person").createAlias("person.wife", "wife"); 
    criterion = Restrictions.eq("wife.age", 19);  
    criteria.add(criterion); 
    criteria.setProjection(projections);
    criteria.setResultTransformer(Transformers.aliasToBean(Person.class)); 
    return criteria.list();
于 2015-09-23T07:54:42.773 回答