Java Hibernate HQL SQL INNER JOIN查询无法正常工作 - Oracle

时间:2014-08-25 23:47:11

标签: java sql hibernate hql

Hibernate / Java新手在这里,任何帮助将不胜感激!

所以......我有一个名为ITEMS的表和一个由

加入的ITEM_OWNER_JOIN表

“itemKey”列和“所有者”列,它是一组字符串值...

在Item.java中我有:

@ForeignKey(name="FK_ITEM_OWNER_FK")
@ElementCollection(targetClass=java.lang.String.class, fetch = FetchType.Eager)
@JoinTable(name= "ITEM_OWNER_JOIN", joinColumns=@JoinColumn(name="itemKey"))
private Set<String> owners = new HashSet<String>();

基本上我正在尝试对业主匹配的结果运行HQL查询 searchText param ....

所以我试过了:

Query q = session.createQuery("select distinct i.itemKey from Item i inner join"+  
          " i.owners o where o.owners like '"+searchText+"'");

我得到一个org.hibernate.QueryException:无法取消引用标量集合元素:所有者[从.....中选择不同的w.workspaceKey]

我试过研究这个例外无济于事...... :(

感谢您的时间!

2 个答案:

答案 0 :(得分:3)

如下所示

<强> HQL

select i 
from Item i 
inner join i.owners io 
where io like 'searchText';

Oracle查询

SELECT Distinct(i.itemKey) 
FROM Item i, ITEM_OWNER_JOIN io 
WHERE i.itemKey  = io.itemKey and io.x like '%%';

其中&#39; x&#39;是列名。

我的应用程序中的工作示例

来自实体:

 @ElementCollection
    @JoinTable(name = "rule_tagged_name", joinColumns = @JoinColumn(name = "re_rule", referencedColumnName = "id"))
    private List<String>         ruleTagNames;

数据库列

RE_RULE NUMBER
RULE_TAG_NAMES

<强> HQL

Select ru FROM Rule ru inner join ru.ruleTagNames rt_name WHERE rt_name in :tagNameList

答案 1 :(得分:0)

尝试使用IN运算符,因为owners是多个。

Query hqlQuery = session.createQuery("select distinct i.itemKey from Item i inner join"+  
      " i.owners o where o.owners in :ownersParam");

然后使用所有者设置值

设置参数owners
Set<String> ownerSet = new HashSet<String>();
ownerSet.add(searchText);

hqlQuery.setParameterList("ownersParam", ownerSet);

//then retrieve result