0

I have the following method where I want a User object with a few fields i.e. name, address.country and a few more.

I am trying to get the the country data in the list but cannot (only name data appears).

public List<User> getActiveUsers() {
    Criteria userCriteria = getSession().createCriteria(User.class, "user")
    .createAlias("address", "address");

    userCriteria.setProjection(Projections.projectionList().add(Projections.property("name"), "name").add(Projections.property("address.country"), "country"));
    userCriteria.setResultTransformer(Transformers.aliasToBean(User.class));

    return userCriteria.list();
}

snippet of People.hbm.xml file

<class name="model.People" table="PEOPLE">
    <id column="PEOPLE_ID" name="id" type="long">
        <generator class="native" />
    </id>

    ....

    <joined-subclass extends="model.People" name="model.User" table="USER">
        <key column="PEOPLE_ID" />
        <many-to-one class="model.BusinessAddress" column="ADDRESS_ID" name="address" />
    </joined-subclass>
</class>

java pojo's

public class User extends People implements Serializable {
    //other fields
    protected BusinessAddress address = new BusinessAddress();
}

public class BusinessAddress extends Address implements Serializable {
    //other fields
}

public abstract class Address implements Serializable {
    //other fields
    public String country;

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

Error stacktrace:

org.hibernate.PropertyNotFoundException: Could not find setter for country on class model.User
 at org.hibernate.property.ChainedPropertyAccessor.getSetter(ChainedPropertyAccessor.java:67)
 at org.hibernate.transform.AliasToBeanResultTransformer.initialize(AliasToBeanResultTransformer.java:118)
 at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:81)
 at org.hibernate.loader.criteria.CriteriaLoader.getResultColumnOrRow(CriteriaLoader.java:158)
 at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:647)
 at org.hibernate.loader.Loader.doQuery(Loader.java:745)
 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
 at org.hibernate.loader.Loader.doList(Loader.java:2449)
 at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
 at org.hibernate.loader.Loader.list(Loader.java:2187)
 at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
 at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706)
 at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
 at dao.PeopleDAO.getActiveUsers(PeopleDAO.java:100)
 .....

country is part of address and not User. I cannot get it to find address. Any help would be appreciated.

Also, the hibnernate query when show_sql = true shows the sql to be correct

Vishal
  • 85
  • 3
  • 10

1 Answers1

0

I saw this solution originally Populate child bean with Transformers.aliasToBean in Hibernate but it was for Hibernate4 where I am using Hibernate3 so I did not pursue it further. However I was still looking for a solution so I thought to see if I could recreate hibernate4 classes, which as it turns out you can.

I created AliasToBeanNestedResultTransformer.java from the link and then this one https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/transform/AliasedTupleSubsetResultTransformer.java and then this one https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/transform/TupleSubsetResultTransformer.java

The classes then compiled and seemed to work fine.

However, my final solution is to actually use Hibernate Search

Community
  • 1
  • 1
Vishal
  • 85
  • 3
  • 10
  • I have a POJO class which has fields in camelCase, say 'discountDetail'. I have defined proper alias in my query i.e. select discount_detail as discountDetail from Inventory where ... but when hibernate returns the result set, it returns 'discountdetail' and thus I get an exception that setter for property 'discountdetail' not found. Any idea about this problem ? – mantri Apr 20 '17 at 12:43