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