【发布时间】:2014-06-04 15:08:02
【问题描述】:
我有以下方法,我想要一个包含几个字段的 User 对象,即 name、address.country 等等。
我正在尝试获取列表中的国家/地区数据但不能(仅显示名称数据)。
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();
}
People.hbm.xml文件的sn-p
<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 的
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;
}
}
错误堆栈跟踪:
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)
.....
国家是地址的一部分,而不是用户。我无法让它找到地址。任何帮助将不胜感激。
另外,show_sql = true 时的 hibnernate 查询显示 sql 是正确的
【问题讨论】:
标签: hibernate hibernate-criteria