将可变参数从 Java 代码传递到 SQL 或 PL/SQL
我需要最多绑定 8 个变量。它们中的每一个都可以为null。 有没有推荐的方法来实现这一目标?我知道我可以简单地检查 null,但这看起来很乏味。
其他详细信息:
我将从 java 代码中调用这个 sql。它可能是使用JPA 2.0 Criteria API编写的,但很可能它将是本机查询。数据库是Oracle 10g,所以我想我也可以使用PL/SQL。
编辑1:
也许标题有点误导,所以我会尽力详细说明。 生成的 SQL 类似于:
<代码>...
哪里 var1 = :var1
和 var2 = :var2
...
AND var = :var8
现在我需要以如下方式绑定来自java代码的参数:nativeQuery.setParameter("var1", var1)
...
nativeQuery.setParameter("var8", var8)
有些参数可以为空,因此不需要绑定它们。但我认为我无法在 SQL 中省略它们。
编辑2: 我希望在您的答案中看到 SQL 或 PL/SQL 过程(如果可以不进行 null 检查)。 事实上,所有这些变量都是同一类型。我认为不可能找到使用 ANSI SQL 的解决方案,但也许有一些 PL/SQL 过程允许使用可变参数?
I need to bind at maximum 8 variables. Each one of them could be null.
Is there any recommended way to achieve this? I know that I could simply check for null, but this seems tedious.
Additional details:
I'm going to call this sql from java code. It may be written using JPA 2.0 Criteria API, but most likely it's going to be a native query. The database is Oracle 10g, so I think I could make use of PL/SQL as well.
Edit1:
Maybe the title is a bit misleading, so I'll try to elaborate.
The resulting SQL would be something like:...
WHERE var1 = :var1
AND var2 = :var2
...
AND var = :var8
Now I need to bind parameters from java code in the way like:nativeQuery.setParameter("var1", var1)
...
nativeQuery.setParameter("var8", var8)
Some parameters could be null, so there is no need to bind them. But I see no way I can omit them in SQL.
Edit2:
I'm expecting to see SQL or PL/SQL procedure in your answers (if it's ever possible without null checking).
In fact, all of these variables are of the same type. I think it's not possible to find a solution using ANSI SQL, but maybe there are some PL/SQL procedures which allow to work with varargs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,使用条件查询是合适的,因为如果我理解正确的话,您需要动态构造 SQL 查询。如果除了 var1 之外的所有变量都为 null,则 where 子句将为
;如果除了 var2 和 var5 之外的所有变量都非 null,那么您将得到
是这样吗?
如果是这样,则执行您计划执行的操作,并使用条件查询动态构建查询。必须做这样的事情:
The use of a criteria query is appropriate in this case, because if I understood correctly, you need to construct the SQL query dynamically. If all the variables except var1 are null, the where clause would be
and if all variables except var2 and var5 are non null you would have
Is that right?
If so, then do what you plan to do, and construct the query dynamically using a criteria query. Something like this must be done:
您没有指定要传递的对象的类型。因此,在这个示例中,我考虑您将传递 Object。
在此示例中,我将参数作为对象列表传递,但如果需要,您可以使用数组 (varargs) 或其他类型的集合。如果客户端向我发送的对象多于预期的对象,它将抛出 IllegalArgumentException。
另外,如果您不想引发异常,您可以继续迭代列表以绑定参数,但使用列表大小或 MAX_SIZE 作为限制。例如:
You don't specify the type of the objects you want to pass. So in this example I'm considering you will pass Object.
In this example I'm passing the arguments as a List of Objects but you could use array (varargs) or another type of collection if you need to. If the client sends me more than the expected objects it will throw an IllegalArgumentException.
Also if you don't want to throw an exception you could just continue and iterate the list to bind the parameters but using the list size or MAX_SIZE as your limit. For example: