我有以下方法,我想要一个带有几个字段的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文件的片段
<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&#39;
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是正确的
答案 0 :(得分:0)
我最初看到了这个解决方案 Populate child bean with Transformers.aliasToBean in Hibernate 但是对于Hibernate4,我使用的是Hibernate3,所以我没有进一步追求它。 但是我仍然在寻找一个解决方案,所以我想看看我是否可以重新创建hibernate4类,事实证明你可以这样做。
我从链接创建了AliasToBeanNestedResultTransformer.java 然后这个 https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/transform/AliasedTupleSubsetResultTransformer.java 然后这个 https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/transform/TupleSubsetResultTransformer.java
然后编译这些类似乎工作正常。
但是,我的最终解决方案是实际使用Hibernate Search