在spring中使用hibernate模板时获取此异常。
Caused by: org.hibernate.QueryException: Not all named parameters have been set: [roledesc, sapid, pass] [
select u from Role as r left join r.users as u where u.sapid=:sapid and u.pass=:pass and r.roledesc=:roledesc
]
at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:291)
at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:275)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:75)
at org.springframework.orm.hibernate3.HibernateTemplate$33.doInHibernate(HibernateTemplate.java:988)
at org.springframework.orm.hibernate3.HibernateTemplate$33.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 30 more
以下是我尝试获取命名查询的类:
public class LoginDaoImpl extends AbstractDaoImpl implements LoginDao {
private static final Logger LOGGER = Logger.getLogger(LoginDaoImpl.class);
@Override
public User loginCheck(User user, Role role) {
LOGGER.debug("Inside validate user:" + user.getPass());
user.setName("");
List<User> employee = new ArrayList<User>();
Query query = (Query) template.findByNamedQuery("findRoleforaUser");
query.setString("sapid", user.getSapid());
query.setString("pass", user.getPass());
query.setString("roledesc", role.getRoledesc());
employee = query.list();
if(employee == null)
{
user.setName("");
}
else if(employee.isEmpty())
{
user.setName("");
}
else if (!(user.getPass().equals(employee.get(0).getPass())))
LOGGER.info("No match found!");
else {
user.setName( employee.get(0).getName());
LOGGER.debug("\nUser \"" + employee.get(0).getName()
+ "\" found and login is successful ");
}
return user;
}
}
这是我的.hbm文件内容,其中定义了命名查询:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping SYSTEM "D:\My HCL JEE Progs\CBA_Quiz\src\dtd\hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hcl.cba.payments.domain.Role" table="roles">
<id name="roleid" type="int" column="ROLEID" />
<property name="roledesc" type="string" column="ROLEDESC" />
<set name="users" cascade="all">
<key column="roleid" />
<one-to-many class="com.hcl.cba.payments.domain.User" />
</set>
</class>
<query name="findRoleforaUser">
<![CDATA[select u from Role as r left join r.users as u where u.sapid=:sapid and u.pass=:pass and r.roledesc=:roledesc]]>
</query>
</hibernate-mapping>
P.S:我尝试在没有hibernate模板的情况下运行/访问查询,并且运行正常,我也以相同的方式设置变量:
Query query = session.findByNamedQuery("findRoleforaUser"); query.setString("sapid", user.getSapid()); query.setString("pass", user.getPass()); query.setString("roledesc", role.getRoledesc()); employee =query.list();
答案 0 :(得分:0)
这应该有效
List<User> employees = new ArrayList<User>();
String[] paramNames = { "sapid", "pass", "roledesc" };
Object[] values = { user.getSapid(), user.getPass(), role.getRoledesc() };
employees = template.findByNamedQueryAndNamedParam("findRoleforaUser", paramNames, values);