休眠查询的例子和预测 [英] Hibernate Query By Example and Projections

查看:99
本文介绍了休眠查询的例子和预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了简短起见,hibernate不支持投影和按例查询?我发现这个帖子:



代码是这样的:

 用户usr = new User(); 
usr.setCity ='TEST';
getCurrentSession()。createCriteria(User.class)
.setProjection(Projections.distinct(Projections.projectionList()
.add(Projections.property(name),name)
.add(Projections.property(city),city)))
.add(Example.create(usr))

就像其他海报所说的那样,生成的sql不断有一个where类引用 y0_ =?而不是this_.city



我已经尝试了几种方法,并且搜索了问题跟踪器,但没有发现任何关于此问题的信息。



使用Projection别名和变形金刚,但它不起作用:

  User usr = new User(); 
usr.setCity ='TEST';
getCurrentSession()。createCriteria(User.class)
.setProjection(Projections.distinct(Projections.projectionList()
.add(Projections.property(name),name)
.add(Projections.property(city),city)))
.add(Example.create(usr))。setResultTransformer(Transformers.aliasToBean(User.class));

有没有人使用过预测和查询?

解决方案

我可以看到你的用户类吗?这只是使用下面的限制。我不明白为什么限制与真正的实例有什么不同(我认为空字段在默认情况下会被忽略)。

 <$ c $ (Projections.projectionList())
.add(Projections.property(name),name)
.add(Projections.property(city),city)))
.add(Restrictions.eq(city,TEST)))
.setResultTransformer(Transformers。 aliasToBean(User.class))
.list();

我从来没有用过alaistToBean,但我只是读过它。您也可以循环播放结果。

  List< Object> rows = criteria.list(); 
for(Object r:rows){
Object [] row =(Object [])r;
类型t =((< Type>)row [0]);



$ b $ p
$ b

如果你不得不这样做,你可以自己手动填充用户。



如果没有更多信息来诊断问题,很难深入研究这个问题。


To make it short: hibernate doesn't support projections and query by example? I found this post:

The code is this:

User usr = new User();
usr.setCity = 'TEST';
getCurrentSession().createCriteria(User.class)
.setProjection( Projections.distinct( Projections.projectionList()
.add( Projections.property("name"), "name")
.add( Projections.property("city"), "city")))
.add( Example.create(usr))

Like the other poster said, The generated sql keeps having a where class refering to just y0_= ? instead of this_.city.

I already tried several approaches, and searched the issue tracker but found nothing about this.

I even tried to use Projection alias and Transformers, but it does not work:

User usr = new User();
usr.setCity = 'TEST';
getCurrentSession().createCriteria(User.class)
.setProjection( Projections.distinct( Projections.projectionList()
.add( Projections.property("name"), "name")
.add( Projections.property("city"), "city")))
.add( Example.create(usr)).setResultTransformer(Transformers.aliasToBean(User.class));

Has anyone used projections and query by example ?

解决方案

Can I see your User class? This is just using restrictions below. I don't see why Restrictions would be really any different than Examples (I think null fields get ignored by default in examples though).

getCurrentSession().createCriteria(User.class)
.setProjection( Projections.distinct( Projections.projectionList()
.add( Projections.property("name"), "name")
.add( Projections.property("city"), "city")))
.add( Restrictions.eq("city", "TEST")))
.setResultTransformer(Transformers.aliasToBean(User.class))
.list();

I've never used the alaistToBean, but I just read about it. You could also just loop over the results..

List<Object> rows = criteria.list();
for(Object r: rows){
  Object[] row = (Object[]) r;
  Type t = ((<Type>) row[0]);
}

If you have to you can manually populate User yourself that way.

Its sort of hard to look into the issue without some more information to diagnose the issue.

这篇关于休眠查询的例子和预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆