Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何正确使用setProjection?_Hibernate - Fatal编程技术网

Hibernate 如何正确使用setProjection?

Hibernate 如何正确使用setProjection?,hibernate,Hibernate,我只想检索UserAccount类中的某些列,因此我有以下代码: UserAccount aUser = (UserAccount)currentSession().createCriteria(UserAccount.class) /* .setProjection(Projections.projectionList() .add(Projections.property

我只想检索UserAccount类中的某些列,因此我有以下代码:

UserAccount aUser = (UserAccount)currentSession().createCriteria(UserAccount.class)
                        /*  .setProjection(Projections.projectionList()
                                    .add(Projections.property("id"))
                                    .add(Projections.property("username"))
                                    .add(Projections.property("email"))
                                    .add(Projections.property("displayname"))) */
                            .add(Restrictions.eq("email", email))
                            .add(Restrictions.eq("password", password))
                            .add(Restrictions.eq("enabled", true))
                            .add(Restrictions.eq("role", Role.CUSTOMER))
                            .uniqueResult();
    System.out.println(aUser);
    return aUser;

我得到了空值作为回报。但是如果我注释掉setProjections,我会让用户获得所有属性。在这种情况下,如何正确使用setProjection?

对于投影,我们应该使用结果转换器和别名:

UserAccount aUser = (UserAccount)currentSession()
    // FROM
    .createCriteria(UserAccount.class)
    // WHERE
    .add(Restrictions.eq("email", email))
    .add(Restrictions.eq("password", password))
    .add(Restrictions.eq("enabled", true))
    .add(Restrictions.eq("role", Role.CUSTOMER))
    // SELECT with alias
    .setProjection(Projections.projectionList()
       .add(Projections.property("id"),"id")             // alias is a must for
       .add(Projections.property("username"),"username") // result transformer
       .add(Projections.property("email"),"email")
       .add(Projections.property("displayname"),"displayname")
    )
    // here we go - this will convert projection into entity
    .setResultTransformer(Transformers.aliasToBean(UserAccount.class))
    .uniqueResult();
检查:


它返回一个对象数组,因此代码应该是:

Object[] rows = (Object[]) session
        .createCriteria(UserAccount.class)
        .setProjection(
                Projections.projectionList()
                        .add(Projections.property("id"))
                        .add(Projections.property("username"))
                        .add(Projections.property("email"))
                        .add(Projections.property("displayname")))
        .add(Restrictions.eq("email", email))
        .add(Restrictions.eq("password", password))
        .add(Restrictions.eq("enabled", true))
        .add(Restrictions.eq("role", Role.CUSTOMER))
        .uniqueResult();

哇,我明白了,你更喜欢对象[],奇怪。。。享受冬眠吧;)