简体   繁体   English

HQL左外部联接缺少映射

[英]HQL left outer join with missing mapping

I am using hibernate/hql to create my querys. 我正在使用hibernate / hql创建查询。 Now I got a problem I'm stuck on for quite some hours now, to understand the situation here is my sorrounding: 现在我遇到一个问题,我已经坚持了几个小时,要了解这里的情况是我的苦恼:

I got 3 Tables I need to get Informations from, with the connection/assignment tables I got 5 overall. 我有3个需要从中获取信息的表,而连接/分配表总共有5个。 The Informations I need are the Key, the Type and the SourceFile, but the special case here is that the sql will be done while importing new data, so I want to first check if the data is already existent, or partially existent. 我需要的信息是密钥,类型和SourceFile,但是这里的特殊情况是在导入新数据时将完成sql,因此我想首先检查数据是否已经存在或部分存在。 My query needs to always give me the key, no matter what Type or SourceFile if the Key itself is already in the Database, also I then only want the key and not the other Informations. 我的查询需要始终为我提供密钥,无论密钥是什么Type或SourceFile,如果密钥本身已经在数据库中,那么我也只需要密钥而不是其他信息。 (Key matches but SourceFile and Type do not, so I want only the key back) If the Key is existent with the exact same Type and SourceFile I want to get all Informations. (键匹配,但SourceFile和Type不匹配,所以我只想要键)如果键存在且Type和SourceFile完全相同,我想获取所有信息。

The Tables are: 这些表是:

(heads up: FK_K_ID is saved as a object with the name key, FK_S_ID is saved as Source and FK_T_ID is saved as Type) (抬起头:使用名称键将FK_K_ID保存为对象,将FK_S_ID保存为Source,将FK_T_ID保存为Type)

Key: 键:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| K_ID        | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| key         | varchar(255) | NO   |     | NULL    |                |
| deleted     | boolean      | NO   |     | FALSE   |                |
+-------------+--------------+------+-----+---------+----------------+

KeyType: 关键字类型:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| KT_ID       | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| FK_K_ID     | bigint(20)   | NO   |     | NULL    |                |
| FK_T_ID     | bigint(20)   | NO   |     | NULL    |                |
| deleted     | boolean      | NO   |     | FALSE   |                |
+-------------+--------------+------+-----+---------+----------------+

Type: 类型:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| T_ID        | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255) | NO   |     | NULL    |                |
| description | varchar(255) | NO   |     | NULL    |                |
| deleted     | boolean      | NO   |     | FALSE   |                |
+-------------+--------------+------+-----+---------+----------------+

KeySource: 关键源:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| KS_ID       | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| FK_K_ID     | bigint(20)   | NO   |     | NULL    |                |
| FK_S_ID     | bigint(20)   | NO   |     | NULL    |                |
| deleted     | boolean      | NO   |     | FALSE   |                |
+-------------+--------------+------+-----+---------+----------------+

Source: 资源:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| S_ID        | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| sourceFile  | varchar(255) | NO   |     | NULL    |                |
| deleted     | boolean      | NO   |     | FALSE   |                |
+-------------+--------------+------+-----+---------+----------------+

and here is what I tried so far: 这是我到目前为止尝试过的:

from KeyType kt
right outer join kt.Key k
with k.name in (...)
where k.deleted = false
and ( kt.Type in (...) or kt is null )

The problem with this one is, it kind of does what I want. 这个问题是我想要的。 but I get all keys in the database and the KeyType only where it matches, else null. 但是我只能在数据库中找到匹配的所有键,并且只能在匹配的KeyType处获取,否则为null。 I don't want to get all keys I just want to get my keys I asked for. 我不想获得所有密钥,而只想获得我要求的密钥。

from KeyType kt
right outer join kt.Key k
with k.name in (...)
where k.deleted = false
and (k.name in (...) and kt.Type in (...) or kt is null )

I don't even know what I tried here to be honest. 我什至不知道我在这里试图说些什么。 I guess I tried to optimize the first query to only give the keys I asked for. 我想我试图优化第一个查询以仅提供我要求的键。

from KeyType kt
right outer join fetch kt.Key k
where k.deleted = false
and (k.name in (...) and kt.Type in (...) or kt is null )

I also tried to use fetch like this or in other variations but didn't work like I need it to. 我也尝试使用类似这样的获取方式或其他变体形式,但没有按我的需要那样工作。

from Key k
left outer join KeyType kt on kt.Key.id = k.id
left outer join KeySource ks on ks.Key.id = k.id
inner join Source s on ks.Source.id = s.id
where k.deleted = false
and k.name in (...)
and ( kt.appType in (...) or kt is null )
and ( s.SourceFile in (...) or s is null )

well as expected this doesn't work. 符合预期,这是行不通的。 I mean its quite simpel to see that it can't work but you try a lot if you can't get a hang on it. 我的意思是,看到它无法正常工作很简单,但是如果无法坚持下去,您可以尝试很多。

I tried many more combinations and variations of querys but with no luck. 我尝试了更多的查询组合和变体,但是没有运气。 The first query is the closest I got. 第一个查询是我得到的最接近的查询。 I Hope somebody can help me. 我希望有人能帮助我。

PS: I can't change the mapping or the entitys now. PS:我现在无法更改映射或实体。 I have to work with what I got. 我必须努力工作。

UPDATE: 更新:

Alright so I am very close to solving the problem. 好的,所以我非常接近解决问题。 My query now looks like this: 我的查询现在看起来像这样:

select k, case when kt.Type not in (...) then null
               else 1 end
from KeyType kt
join kt.Key k
where k.name in (...)

Now the only thing I want to to is exchange the 1 with the actual object. 现在,我唯一想做的就是将1与实际对象交换。 But if I do this I get the error "org.hibernate.exception.GenericJDBCException: could not execute query" (running on an oracle db) 但是,如果执行此操作,则会收到错误“ org.hibernate.exception.GenericJDBCException:无法执行查询”(在oracle数据库上运行)

Can someone tell me how to solve it? 有人可以告诉我如何解决吗?

For my case and sorrounding the way I wanted to do it is not possible. 就我的情况而言,无法确定我想要的方式。

So I asked coworkers again and we came to the solution we have to do it in single querys. 因此,我再次询问同事,我们得出了必须在单个查询中完成的解决方案。

Just saying this for anyone passing by this post with the same table configuration/problem. 只是对通过此帖子且具有相同表配置/问题的任何人说这话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM