寻找HQL构建器(休眠查询语言) [英] Looking for an HQL builder (Hibernate Query Language)

查看:60
本文介绍了寻找HQL构建器(休眠查询语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 HQL 的构建器Java.我想摆脱像这样的事情:

I'm looking for a builder for HQL in Java. I want to get rid of things like:

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上搜索了一下,但找不到.

I googled a bit, and I couldn't find one.

我写了一篇简短的&哑巴HqlBuilder可以满足我现在的需求,但是我很想找到一个比我一个拥有更多用户和测试的人.

I wrote a quick & dumb HqlBuilder that suits my needs for now, but I'd love to find one that has more users and tests than me alone.

注意:我想能够做类似这样的事情,而我使用Criteria API却做不到:

Note: I'd like to be able to do things like this and more, which I failed to do with the Criteria API:

select stock
from com.something.Stock as stock, com.something.Bonus as bonus
where stock.someValue = bonus.id

即.从奖金"表中选择属性someValue指向 any 奖金的所有股票.

ie. select all stocks whose property someValue points to any bonus from the Bonus table.

谢谢!

推荐答案

@

@Sébastien Rocca-Serra
Now we're getting somewhere concrete. The sort of join you're trying to do isn't really possible through the Criteria API, but a sub-query should accomplish the same thing. First you create a DetachedCriteria for the bonus table, then use the IN operator for 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在所有表中都不唯一.但是您的查询将遭受同样的缺陷.

The only downside would be if you have references to different tables in someValue and your ID's are not unique across all tables. But your query would suffer from the same flaw.

这篇关于寻找HQL构建器(休眠查询语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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