我有两个域名如下
WishList{
int id;
@ManyToOne
Item item;
}
Item{
int id;
int productCategory;
String productName;
}
现在假设我有产品
(1,1,Mobile )
(2,2,Microwave)
(3,3,flashDrive)
(4,1, Monitor)
现在如何在WishList
中按查询进行分组,以便我得到
(1,1,Mobile )
(4,1, Monitor)
(2,2,Microwave)
(3,3,flashDrive)
我正在使用Hibernate Criteria
怎么做?
答案 0 :(得分:2)
如果您使用的是休眠Criteria
,则可以为Item
创建alias
,例如
Criteria criteria = hibernetSession.createCriteria(WishList.class);
criteria.createAlias("item", "itemAlias")
.setProjection(Projections.projectionList()
.add(Projections.property("id"),"id")
.add(Projections.property("itemAlias.id"),"itemAlias.id")
.add(Projections.property("itemAlias.productCategory"),"itemAlias.productCategory")
.add(Projections.property("itemAlias.productName"),"itemAlias.productName")
.add(Projections.groupProperty("itemAlias.productName")))
.setResultTransformer(Transformers.aliasToBean(WishList.class));
List<WishList> itemList = criteria.list();