程序跑起来的时候会报错误~~
调用:Hibernate.update("Z","C")
用的MySql数据库
会报以下错误:
Parameter name does not exist as a named parameter in [from Client as client where client.name=?]
数据库已经检查,存在client表,且name字段为Z的数据。
具体代码如下:
public static void update(String oldName,String newName) {
Session session=null;//Session
Transaction ts=null;//Transaction

try {
//1 update
session=HibernateUtil.getSession();//this is get Session
ts=session.beginTransaction();//this is beginTransaction

//2 select the oldName,then change the oldName to newName 
String select_hql="from Client as client where client.name=?";
                          //Client is a  Bean
    Query query_select=session.createQuery(select_hql);
    query_select.setString("name",oldName);
    List<Client> result_list=query_select.list();//from the oldName get the right Client
    for(Client cl:result_list){
     System.out.println("the oldName is : "+cl.getName()+"   and now is change to newName");
     cl.setName(newName);//the client is in session,change to newName
    }
    ts.commit();
    
    //3 select resaults
       
} finally {
// TODO Auto-generated catch block
if (session != null)
session.close();//close the session
}
}那个哥们儿给小弟看看,小弟Hibernate新手,刚学,不胜感激

解决方案 »

  1.   

    ="from Client as client where client.name=?";
    改写成="select client.name from Client as client where client.name=?";
      

  2.   

    是你SQL语句写错了。你要确定Client表里有name字段,你的本意应该是:
    select * from Client client where client.name = ?;
    注意在SQL语句实际应用中最好是列出具体查询字段而不是直接写 ‘*’。且在相应字段前加表别名前缀。
      

  3.   

    突然想到你用 AS的原因,可能是跟列别名混淆了。
    select access as data from tablename;
    这句执行效果展示列名是data.而表别名是:直接 表名 接 表别名。
    select t.access as data from tablename t;
      

  4.   

    注意在SQL语句实际应用中最好是列出具体查询字段而不是直接写 ‘*’。且在相应字段前加表别名前缀。
      

  5.   

    那个语句是hql语句,不是sql语句,是有区别的,select是不行的,然后,加了具体的列,但是取的时候还是会报哪个异常
      

  6.   

    楼主可以清晰的看到
    //2 select the oldName,then change the oldName to newName  
    String select_hql="from Client as client where client.name=?";
      //Client is a Bean
    Query query_select=session.createQuery(select_hql);
    query_select.setString("name",oldName);
    很明显就是不是对应的!
    是不是应该修改一下呀!
      

  7.   

    楼上这个虽然说的不完全对,但是也提醒我了一下
    String select_hql="from Client as client where client.name=?";
    query_select.setString(0,oldName);
    这里应该是用序列号,来代替问好的位置,序列号默认是从0开始的,然后把oldName赋值给0对应的问号
      

  8.   

    hql里面应该使用变量声明。
    即:的方式String select_hql="from Client as client where client.name=:name";
    query_select.setString("name",oldName);
    使用?的方式不能把作为参数名来赋值