Java 两次使用休眠命名参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16251491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using hibernate named parameter twice
提问by DmiN
Assumed i have the following HQL
假设我有以下 HQL
EntityManager.createQuery("SELECT a FROM a WHERE a.b = :par OR a.c = :par").setParameter("par", obj);
seems not to work. Does anybody have an idea how to solve this problem remain using only one parameter?
似乎不起作用。有没有人知道如何只使用一个参数来解决这个问题?
采纳答案by Dhivya
setParameter(String name,Object val)
This is used to bind a value to the named parameter. But a name can occur multiple times in a query that doesn't matter. So check once whether you have really data for that query.
这用于将值绑定到命名参数。但是一个名称可以在一个无关紧要的查询中出现多次。因此,请检查一次您是否拥有该查询的真正数据。
check the documentation here
检查文档here
Some main text from that documentation
该文档中的一些主要文本
Named query parameters are tokens of the form :name in the query string. A value is bound to the integer parameter :foo by calling setParameter("foo", foo, Hibernate.INTEGER); for example. A name may appear multiple times in the query string.
命名查询参数是查询字符串中 :name 形式的标记。通过调用 setParameter("foo", foo, Hibernate.INTEGER); 将一个值绑定到整数参数 :foo 例如。一个名字可能在查询字符串中出现多次。
If still u don't get the result then just try with using two names and set it
如果仍然没有得到结果,那么只需尝试使用两个名称并设置它
EntityManager.createQuery("SELECT a FROM a WHERE a.b = :par1 OR a.c = :par2").setParameter("par1", obj).setParameter("par2", obj);
EntityManager.createQuery("SELECT a FROM a WHERE a WHERE ab = :par1 OR ac = :par2").setParameter("par1", obj).setParameter("par2", obj);
回答by Pramod
setParameter(String name,Object val)
Replaces all occurence of name, specified in the query.
替换查询中指定的所有名称。