两次使用hibernate命名参数 [英] Using hibernate named parameter twice

查看:114
本文介绍了两次使用hibernate命名参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下HQL

  EntityManager.createQuery(SELECT a FROM a WHERE ab =:par OR ac = :par)。setParameter(par,obj); 

似乎不起作用。是否有人有一个想法如何解决这个问题仍然只使用一个参数?

解决方案

  setParameter(String name,Object val)

这用于将值绑定到指定参数。但是名称可以在无关紧要的查询中多次出现。因此,请检查一次您是否有真正的查询数据。



检查文档此处



有些来自该文档的主要文本



命名查询参数是查询字符串中形式为:name的标记。通过调用
setParameter(foo,foo,Hibernate.INTEGER)将值绑定到整型参数:foo;例如
。一个名字可能会在查询字符串中出现多次。



如果仍然没有得到结果,那么试试使用两个名字并将其设置为


EntityManager.createQuery(SELECT a FROM a WHERE ab =:par1 OR ac =:par2)。setParameter(par1,obj ).setParameter(par2,obj);


Assumed i have the following 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?

解决方案

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

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.

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);

这篇关于两次使用hibernate命名参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆