JPQL 合并非标量?
我有一个存储应用程序配置参数的表,如下所示:
setName | key | value
----------------------------------
dev | FooEnabled | true
dev | BarEnabled | true
qa | FooEnabled | false
...etc...
每个参数(键/值组合)都属于特定的“配置集”。选择特定集合中的所有属性很容易:
SELECT p Parameter p WHERE p.setName = :name
因此,如果我想要名为 "dev"
的配置集中的所有参数,我会返回
dev | FooEnabled | true
dev | BarEnabled | true
现在我想选择两个集合的并集:
SELECT p Parameter p WHERE p.setName = :name1 OR p.setName = :name2
...但通过为name1
集中的属性赋予比name2
集中的属性更高的优先级来消除具有重复键的行。如果 name1
是 "qa"
并且 name2
是 "dev"
,那么这将返回
dev | FooEnabled | true
qa | BarEnabled | false
为了澄清,查询应该选择:
- 名称为
name2
的集合中的每个属性,除非... - 在名称为
name1
的集合中存在具有相同键的属性,在这种情况下... - 选择带有
setName = :name1
的行而不是行与setName = :name2
什么 JPQL 查询可以有效地完成此操作? 如果 COALESCE
使用非标量值,那么我想像这样的事情可以工作:
SELECT
COALESCE(x, y)
FROM Property x, Property y
WHERE
x.setName = :name1 AND
y.setName = :name2 AND
x.key = y.key
但是COALESCE
只接受标量值。有什么想法吗?
其他查询尝试/失败:
SELECT
CASE
WHEN (NOT EXISTS (SELECT y FROM Property y WHERE y.setName = :name1 AND y.key = x.key))
THEN x
ELSE y
END
FROM Property x
WHERE
x.setName = :name2
我也有兴趣以相同的方式通过 key
选择单独的行,我想它看起来像下面这样(如果只有 COALESCE 适用于非标量):
SELECT
COALESCE(y, x)
FROM Property x, Property y
WHERE
x.key = :key AND
x.setName = :name1 AND
y.key = :key AND
y.setName = :name2
I have a table storing application configuration parameters like so:
setName | key | value
----------------------------------
dev | FooEnabled | true
dev | BarEnabled | true
qa | FooEnabled | false
...etc...
Each parameter, a key/value combination, belongs to a particular "configuration set." It's easy to select all properties in a particular set:
SELECT p Parameter p WHERE p.setName = :name
So if I wanted all the parameters in config set named "dev"
I'd get back
dev | FooEnabled | true
dev | BarEnabled | true
Now I'd like to select the union of two sets:
SELECT p Parameter p WHERE p.setName = :name1 OR p.setName = :name2
...but eliminate rows with duplicate keys by giving properties in set with name1
higher precedence than properties in set with name2
. If name1
was "qa"
and name2
was "dev"
, then this would return
dev | FooEnabled | true
qa | BarEnabled | false
To clarify, the query should SELECT:
- Every property from set with name
name2
, except when... - There is a property with the same key in set with name
name1
, in which case... - Select the row with
setName = :name1
instead of the row withsetName = :name2
What JPQL query will do this efficiently? If COALESCE
worked with non-scalar values, then I imagine that something like this could work:
SELECT
COALESCE(x, y)
FROM Property x, Property y
WHERE
x.setName = :name1 AND
y.setName = :name2 AND
x.key = y.key
but COALESCE
only accepts scalar values. Any ideas?
Other queries attempted/failed:
SELECT
CASE
WHEN (NOT EXISTS (SELECT y FROM Property y WHERE y.setName = :name1 AND y.key = x.key))
THEN x
ELSE y
END
FROM Property x
WHERE
x.setName = :name2
I'm also interested in selecting individual rows in the same manner, by key
, which I suppose would look like the following (if only COALESCE worked for non-scalars):
SELECT
COALESCE(y, x)
FROM Property x, Property y
WHERE
x.key = :key AND
x.setName = :name1 AND
y.key = :key AND
y.setName = :name2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以创建一个基本上具有以下结构的查询:(
仅是我的想法,没有尝试过)
Possibly you can create a query with basically following structure:
(Just from my mind without having it tried)