Java SQLGrammarException:无法执行查询

Java SQLGrammarException:无法执行查询,java,sql,hibernate,struts2,hql,Java,Sql,Hibernate,Struts2,Hql,我使用Struts2和Hibernate,当我用stringtest搜索数据时,会出现以下错误,但当我用numeric111搜索数据时,它对我有效。我从bean类和bean类中定义的字符串类型属性中获取这个值 以下是我提供的代码: publicstringretrieverecords() { 字符串empId=p.getEmpId(); 字符串参数值=”; if(empId!=null) 如果(!(empId.isEmpty()) paramValue=“其中b.empId=“+emp

我使用Struts2和Hibernate,当我用string
test
搜索数据时,会出现以下错误,但当我用numeric
111
搜索数据时,它对我有效。我从bean类和bean类中定义的字符串类型属性中获取这个值

以下是我提供的代码:

publicstringretrieverecords()
{    
字符串empId=p.getEmpId();
字符串参数值=”;
if(empId!=null)
如果(!(empId.isEmpty())
paramValue=“其中b.empId=“+empId;
字符串empName=p.getEmployeeName();
如果(empName!=null&&empName!=“”)
{
if(!(empName.isEmpty()){
如果(参数值==“”)
paramValue=“其中b.employeeName=“+empName;
其他的
paramValue=paramValue+”和b.employeeName=“+empName;
}
}
System.out.println(“=============paramvalues===”+paramvalues);
recList=(List)session.createQuery(“来自RequestBean b”+paramValue).List();
setAttribute(“rec”,recList);
System.out.println(“获取大小”+重新列表);
回归成功;
}
Bean类:

公共类RequestBean{
私人长id;
私有字符串empId;
私有字符串employeeName;
私有字符串employeeType;
私有字符串personnalNumber;
私人字符串联系人号码;
私有字符串companyName;
私有字符串地址;
私人字符串注释;
私有字符串empStatus=“E”;
私有日期joiningDate=null;
创建私人日期;
/*************吸气剂************************/
公共长getId(){
返回id;
}
公共字符串getEmpId(){
返回empId;
}
公共字符串getEmployeeName(){
返回员工姓名;
}
公共字符串getEmployeeType(){
返回员工类型;
}
公共字符串getPersonnalNumber(){
返回人名号码;
}
公共字符串getContactNumber(){
返回联系人号码;
}
公共字符串getCompanyName(){
返回公司名称;
}
公共字符串getAddress(){
回信地址;
}
公共字符串getComments(){
回帖;
}
公共日期getJoiningDate(){
返回接合日期;
}
公共字符串getEmpStatus(){
返回状态;
}
公共日期getCreated(){
创建回报;
}
/*******************二传手***************************/
公共无效集合id(长id){
this.id=id;
}
public void setEmpId(字符串empId){
this.empId=empId;
}
public void setEmployeeName(字符串employeeName){
this.employeeName=employeeName;
}
公共void setEmployeeType(字符串employeeType){
this.employeeType=employeeType;
}
公共void setPersonnalNumber(字符串personnalNumber){
this.personnalNumber=personnalNumber;
}
public void setContactNumber(字符串contactNumber){
this.contactNumber=contactNumber;
}
public void setCompanyName(字符串companyName){
this.companyName=companyName;
}
公共无效设置地址(字符串地址){
this.address=地址;
}
公共无效设置备注(字符串备注){
这个。备注=备注;
}
公共无效设置加入日期(加入日期){
this.joiningDate=joiningDate;
}
public void setEmpStatus(字符串empStatus){
this.empStatus=empStatus;
}
创建公共作废集(创建日期){
this.created=created;
}
}
映射:


错误:

org.hibernate.exception.sqlgrammareexception:无法执行查询
org.hibernate.exception.sqlstatecoverter.convert(sqlstatecoverter.java:90)
org.hibernate.exception.jdbceptionhelper.convert(jdbceptionhelper.java:66)
org.hibernate.loader.loader.doList(loader.java:2231)
org.hibernate.loader.loader.listIgnoreQueryCache(loader.java:2125)
org.hibernate.loader.loader.list(loader.java:2120)
org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
retrieveRecords(RequestControllerDAO.java:60)
sun.reflect.GeneratedMethodAccessor76.invoke(未知源)
sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)
java.lang.reflect.Method.invoke(未知源)
ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:891)
ognl.OgnlRuntime.CallApproverMethod(OgnlRuntime.java:1293)
ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
getValueBody(ASTMethod.java:90)
ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
ognl.SimpleNode.getValue(SimpleNode.java:258)
ognl.ognl.getValue(ognl.java:494)
ognl.ognl.getValue(ognl.java:458)

由于Hibernate生成的SQL查询具有错误的SQL语法,因此引发了
SQLGrammarException
。构建查询的方式是错误的,不应该将值(尤其是字符串值)连接到结果查询,因为这样的代码容易受到攻击。相反,您可以在查询字符串中使用参数

String empId=p.getEmpId();
字符串参数值=”;
if(empId!=null&&!empId.isEmpty())
paramValue=“其中b.empId=:empId”;
字符串empName=p.getEmployeeName();
if(empName!=null&&!empName.isEmpty()){
if(参数值
    "where employeeName=Name" 
    "where employeeName='Name'".
    "where b.employeeName= ' " +empName+ " ' ";