我用hql语句查询,只要里面没有  ‘:’这种程序都运行的好好的,但是像这种语句“  from  Msg m where m= :MsgToSearch”里面只要‘:’需要赋值的都出错为什么??
下面是Msg类
package com.menglei.shili;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table; 
@Entity
@Table(name="Msg")
public class Msg {
private int id;
private String count;
private Topic topic;
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}


@ManyToOne
@JoinColumn(name="Tid")
public Topic getTopic() {
return topic;
}
public void setTopic(Topic topic) {
this.topic = topic;
}
}
  这是测试类中代码:
 Query q=session.createQuery("  from  Msg m where m.id= :MsgToSearch");
Msg m=new Msg();
m.setId(1);
q.setParameter("MsgToSearch", m.getId());

Msg  mResult =(Msg)q.uniqueResult();
System.out.print(mResult.getCount()); 

解决方案 »

  1.   

    看具体报错在哪一行;
    另外,用uniqueResult方法要确定你的HQL是返回一行,如果返回多行,那不要用这个方法,要用list()方法.
      

  2.   

    Query q=session.createQuery("  from  Msg m where m.id=:MsgToSearch");
            Msg m=new Msg();
            m.setId(1);
            q.setParameter("MsgToSearch", m.getId());//这样做是没有问题的
            
            Msg  mResult =(Msg)q.uniqueResult();//你用uniqueResult()方法,要确保你查询的结果集只有一天记录,否则报错哦。
            System.out.print(mResult.getCount());
      

  3.   

    .uniqueResult()这个方法只返回一行 所以你要看看