内容简介:翻译自:https://stackoverflow.com/questions/56843/looking-for-an-hql-builder-hibernate-query-language
构建器.我想摆脱像这样的事情:
StringBuilder builder = new StringBuilder()
.append("select stock from ")
.append( Stock.class.getName() )
.append( " as stock where stock.id = ")
.append( id );
我宁愿有类似的东西:
HqlBuilder builder = new HqlBuilder()
.select( "stock" )
.from( Stock.class.getName() ).as( "stock" )
.where( "stock.id" ).equals( id );
我google了一下,我找不到一个.
我写了一个快速&愚蠢的HqlBuilder现在适合我的需求,但我很想找到一个比我单独拥有更多用户和测试的人.
注意:我希望能够做到这样的事情以及更多,我没有使用Criteria API:
select stock from com.something.Stock as stock, com.something.Bonus as bonus where stock.someValue = bonus.id
即.选择其属性someValue指向奖金表中任何奖金的所有股票.
谢谢!
现在我们得到了具体的地方.您尝试做的联接实际上并不是通过Criteria API实现的,但是子查询应该完成同样的事情.首先,为奖励表创建一个DetachedCriteria,然后使用IN运算符获取someValue.
DetachedCriteria bonuses = DetachedCriteria.forClass(Bonus.class);
List stocks = session.createCriteria(Stock.class)
.add(Property.forName("someValue").in(bonuses)).list();
这相当于
select stock from com.something.Stock as stock where stock.someValue in (select bonus.id from com.something.Bonus as bonus)
唯一的缺点是,如果您在someValue中引用了不同的表,并且您的ID在所有表中都不是唯一的.但是你的查询会遇到同样的缺陷.
翻译自:https://stackoverflow.com/questions/56843/looking-for-an-hql-builder-hibernate-query-language
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 医疗领域构建自然语言处理系统的经验教训
- 换Make来构建你的Go语言项目吧
- Goa 1.3.0 发布,Go 语言构建微服务框架
- Goa 1.4.0 发布,Go 语言构建微服务框架
- 使用Gin框架构建一个简单的注册登录后台(Go语言)
- go语言[3]-数据结构-递归树构建17亿数据的内存模型
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Design and Analysis of Distributed Algorithms (Wiley Series on P
Nicola Santoro / Wiley-Interscience / 2006-10-27 / USD 140.95
This text is based on a simple and fully reactive computational model that allows for intuitive comprehension and logical designs. The principles and techniques presented can be applied to any distrib......一起来看看 《Design and Analysis of Distributed Algorithms (Wiley Series on P》 这本书的介绍吧!