junit测试方法 老是报java.lang.reflect.UndeclaredThrowableException

meng020712 2012-10-23 06:52:35
我用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());
...全文
350 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
a8509190 2012-10-25
  • 打赏
  • 举报
回复
.uniqueResult()这个方法只返回一行 所以你要看看
  • 打赏
  • 举报
回复
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());
brightyq 2012-10-25
  • 打赏
  • 举报
回复
看具体报错在哪一行;
另外,用uniqueResult方法要确定你的HQL是返回一行,如果返回多行,那不要用这个方法,要用list()方法.
zhao_945 2012-10-24
  • 打赏
  • 举报
回复
试试:=看看好不好使

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧