Hibernate Criteria如何在HashSet中设置属性结果

时间:2015-01-14 10:38:23

标签: java hibernate-criteria

我在将属性设置为HashSet字段时遇到了问题。

这是我的代码:

public class PhotoSetModel {


        private Integer setId;
         private String setName;
         private Date date;
         private Integer userId;
         Set<PhotoSetDetailModel> photosetDetail= new HashSet<PhotoSetDetailModel>();

       // Getter setter method

}

public class PhotoSetDetailModel {
    private Integer photoId;
    private String storageLink;

  // Getter setter method
}

在DAO:

public List<PhotoSetModel> photosetListWithPhotos(int userId) {
        List<PhotoSetModel> list = null;
        Criteria criteria = null;
        Session session = null;
        try{
            session = sessionFactory.getCurrentSession();
            criteria = session.createCriteria(PhotosetDef.class);

            criteria.createAlias("user", "user");
            criteria.createAlias("photosetPhotos", "photosetPhotos");
            criteria.createAlias("photosetPhotos.photos", "photos");

            criteria.add(Restrictions.eq("user.userId", userId));


            ProjectionList pList = Projections.projectionList();
            pList.add(Projections.property("photosetDefId"),"setId");
            pList.add(Projections.property("photosetName"),"setName");
            pList.add(Projections.property("photosetDate"),"date");
            pList.add(Projections.property("user.userId"),"userId");
            pList.add(Projections.property("photos.photoId"),"photosetDetail.photoId");
            pList.add(Projections.property("photos.storageLink"),"photosetDetail.storageLink");


            criteria.setProjection(pList);
            criteria.setResultTransformer(Transformers.aliasToBean(PhotoSetModel.class));
            list = criteria.list();
        }catch(Exception e){
            e.printStackTrace();
        }
        return list;

我发现错误发生在:

pList.add(Projections.property("photos.photoId"),"photosetDetail.photoId");
pList.add(Projections.property("photos.storageLink"),"photosetDetail.storageLink");

例外情况是: org.hibernate.PropertyNotFoundException:找不到类com.photosite.model.PhotoSetModel上的photosetDetail.photoId的setter

如何为HashSet设置属性?

我期望的json格式结果是:

[{setId : 1,setName : "name", date : "12.05.2015", PhotoSetDetailModel:[{photoId:1,storageLink:"link1"},{photoId:2,storageLink:"link2"}]}, {setId : 2,setName : "name2", date : "12.05.2015", PhotoSetDetailModel:[{photoId:3,storageLink:"link3"},{photoId:4,storageLink:"link4"}]}]

0 个答案:

没有答案