Simple Hibernate aliasToBean () not working

The following code does not work for me:

List results = getSession().createCriteria(User.class)
    .setProjection(Projections.projectionList()
            .add(Projections.property("userName"))
    )
    .setResultTransformer(Transformers.aliasToBean(UserSummary.class))
    .list();


Funny though, if I delete setResultTransformer(), I get a list of usernames returned back.

Here is my UserSummary class:

public class UserSummary {

    private String userName;
    private String clickUrl;
    private Integer id;

    public UserSummary() {}

    public UserSummary(Integer id, String userName) {
        this.id = id;
        this.userName = userName;
        this.clickUrl = clickUrl;
    }

    public String getUserName() {
        return userName;
    }

    public String getClickUrl() {
        return clickUrl;
    }

    public void setClickUrl(String clickUrl) {
        this.clickUrl = clickUrl;
    }

    public Integer getId() {
        return id;
    }
}


Thoughts?

+5
source share
2 answers

Fixed.

I had to change my projection to look like this.

.add(Projections.property("userName"), "userName")

Odd ... but everything that works, I think.

+8
source
.add(Projections.property("userName"), "userName");

.. is correct in this situation.

aliasToBean() ResultTransformer, aliased UserSummary . add() - . "userName", UserSummary, . . Hibernate lib , / UserSummary . aliasToBean() , - . , :

private String userFullName;

public UserSummary(Integer id, String userFullName) {
   this.id = id;
   this.userFullName = userFullName;
   this.clickUrl = clickUrl;
}

public String getUserFullName() {
   return userFullName;
}

add() :

.add(Projections.property("userName"), "userFullName");

userName User userFullName UserSummary , .

+5

All Articles