是否可以获取 Hibernate sqlRestriction 的连接表的 SQL 别名?
我有一个 Person 类,它有一个别名的 String 集合,代表该人可能使用的其他名称。例如,克拉克·肯特可能有别名“超人”和“钢铁之躯”。德怀特·霍华德还有一个别名“超人”。
@Entity
class Person {
@CollectionOfElements(fetch=FetchType.EAGER)
Set<String> aliases = new TreeSet<String>();
Hibernate 在我的数据库中创建两个表:Person 和 Person_aliases。 Person_aliases 是一个包含 Person_id 和 element 列的连接表。假设 Person_aliases 具有以下数据,
--------------------------------
| Person_id | element |
--------------------------------
| Clark Kent | Superman |
| Clark Kent | Man of Steel |
| Dwight Howard | Superman |
| Bruce Wayne | Batman |
--------------------------------
我想对所有别名为“Superman”的人进行休眠条件查询。
由于原因太长,无法在此列出,我真的很想将其设为 Criteria 查询,而不是 HQL 查询(除非可以在 Criteria 对象上添加 HQL 限制,在这种情况下,我洗耳恭听)或原始查询SQL 查询。因为根据 如何使用 Hibernate Criteria 查询具有 String 集合中的值的对象? 不可能使用 CriteriaAPI 引用值类型集合的元素,我想我应该诉诸于添加 SqlRestriction我的标准对象。
Criteria crit = session.createCriteria(Person.class);
crit.add(Restrictions.sqlRestriction("XXXXX.element='superman'");
希望 Hibernate 能够创建像这样的 SQL 语句,
select *
from
Person this_
left outer join
Person_aliases aliases2_
on this_.id=aliases2_.Person_id
where
XXXXX.element='superman'
但是,我需要在 SQL 查询中使用 Person_aliases 表的表别名填写 XXXXX,在本例中为“aliases2_”。我注意到,如果我需要引用 Person 表别名,我可以使用 {alias}。但这不起作用,因为 Person 是此 Criteria 的主表,而不是 Person_aliases。
XXXXX 应该填什么?如果没有像 {alias} 这样好的替换标记,那么有没有办法让 hibernate 告诉我该别名是什么?我注意到一个名为generateAlias() org.hibernate.util.StringHelper 类的方法。这能帮助我预测别名是什么吗?
我真的非常想避免硬编码“aliases2_”。
感谢您抽出时间!
I have a Person class which has a String collection of aliases representing additional names that person may go by. For example, Clark Kent may have aliases "Superman" and "Man of Steel". Dwight Howard also has an alias of "Superman".
@Entity
class Person {
@CollectionOfElements(fetch=FetchType.EAGER)
Set<String> aliases = new TreeSet<String>();
Hibernate creates two tables in my database, Person and Person_aliases. Person_aliases is a join table with the columns Person_id and element. Let's say Person_aliases has the following data
--------------------------------
| Person_id | element |
--------------------------------
| Clark Kent | Superman |
| Clark Kent | Man of Steel |
| Dwight Howard | Superman |
| Bruce Wayne | Batman |
--------------------------------
I want to make a hibernate Criteria query for all persons who go by the alias of "Superman".
For reasons too long to list here, I'd really like to make this a Criteria query, not an HQL query (unless it's possible to add an HQL restriction on a Criteria object, in which case I'm all ears) or a raw SQL query. Since according to How do I query for objects with a value in a String collection using Hibernate Criteria? it is impossible to refer to elements of value-type collections using the CriteriaAPI I thought I'd resort to adding an SqlRestriction on my criteria object.
Criteria crit = session.createCriteria(Person.class);
crit.add(Restrictions.sqlRestriction("XXXXX.element='superman'");
in the hopes that Hibernate will create an SQL statement like
select *
from
Person this_
left outer join
Person_aliases aliases2_
on this_.id=aliases2_.Person_id
where
XXXXX.element='superman'
However, I need to fill in the XXXXX with the table alias for the Person_aliases table in the SQL query, which in this case would be 'aliases2_'. I noticed that if I needed the reference to the Person table alias I could use {alias}. But this won't work because Person is the primary table for this Criteria, not Person_aliases.
What do I fill in for the XXXXX? If there is no nice substition token like {alias} then is there a way I could get hibernate to tell me what that alias is going to be? I noticed a method called generateAlias() org.hibernate.util.StringHelper class. Would this help me predict what the alias would be?
I'd really, really like to avoid hard coding 'aliases2_'.
Thanks for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
正如 xmedeko 提到的,当你想做的时候:
你需要做的是:
这已经为我解决了类似的问题,而无需去 HQL
as xmedeko alludes to, when you want to do:
you need to instead do:
This has solved similar problems for me without going to HQL
Criteria API 似乎不允许查询元素集合,请参阅 HHH -869(仍然开放)。因此,要么尝试建议的解决方法(我没有这样做),要么切换到 HQL。以下 HQL 查询可以工作:
It seems that the Criteria API doesn't allow to query collections of elements, see HHH-869 (which is still open). So either try the suggested workaround - I didn't - or switch to HQL. The following HQL query would work:
尝试创建另一个标准,例如
try to create another Criteria like
此链接可以帮助您吗?它建议:
May this link help you? It advices:
这个问题实际上很老了,但是由于我今天遇到了同样的问题并且没有答案满足我的需求,我根据 Brett Meyer 对 HHH-6353,这个问题不会得到解决。
基本上,我扩展了 SQLCriterion 类能够处理多个基表别名。出于方便起见,我编写了一个小型容器类,它将用户给定的别名与匹配的子标准实例链接起来,以便能够用为子标准创建的别名 hibernate 替换用户给定的别名。
下面是 MultipleAliasSQLCriterion 类的代码,
我这样使用它,
但不需要在创建条件时指定别名 - 您也可以在 SQL 限制中指定它并将其传递到容器。
The question is actually quite old, but since I encountered the same problem today and no answer satisfied my needs, I came up with the following solution, based on the comment by Brett Meyer on HHH-6353, that this issues won't be fixed.
Basically, I extended the SQLCriterion class to be able to handle more than the base table alias. For convenience reasons I wrote a small container class that links the user given alias with the matching subcriteria instance to be able to replace the user given alias with the alias hibernate created for the subcriteria.
Here is the code of the MultipleAliasSQLCriterion class
I use it like this
But there is no need to specify the alias at criteria creation - you can also specify it at the SQL restriciton and pass it to a container.
查看此 Hibernate bug 并使用附加文件:
See this Hibernate bugs and use attached files:
org.hibernate.criterion.CriteriaQuery
有一个方法getColumnsUsingProjection
,它为您提供别名列名称。您可以使用 org.hibernate.criterion.PropertyExpression 作为示例来实现自己的 Criterion。
org.hibernate.criterion.CriteriaQuery
has a methodgetColumnsUsingProjection
which gives you the aliased column name.You could implement your own
Criterion
, usingorg.hibernate.criterion.PropertyExpression
as an example.