hibernate!求高手指教!多谢!!!

sinat_35381660 2016-07-11 05:17:22
新手正在学习用hibernate做简单的sql增删改查操作,已经实现了增加,可是查询编号的时候出了问题,求大神们帮忙看一下,谢谢了
Session session = null;
Configuration conf = null;
SessionFactory sf = null;
Transaction tx=null;
news n = null;
try{
conf=new Configuration().configure();
sf=conf.buildSessionFactory();
session=sf.openSession();
tx = session.beginTransaction();
n = (news) session.load(news.class, new Byte("5"));
session.delete(n);
tx.commit();
}catch(HibernateException ex){
ex.printStackTrace();
tx.rollback();
}finally{
if(session!=null){
session.close();
}

}
...全文
126 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
WorkLee 2016-07-12
  • 打赏
  • 举报
回复
那个错不已经告诉你了,id有问题呗
阳光越来越暖 2016-07-11
  • 打赏
  • 举报
回复
n = (news) session.load(news.class, new Byte("5")); 类名首字母大写 不应该是 n = (News) session.load(News.class, 5);
NANU-NANA 2016-07-11
  • 打赏
  • 举报
回复
id 类型是integer 不是byte
sinat_35381660 2016-07-11
  • 打赏
  • 举报
回复
这个是报错 org.hibernate.TypeMismatchException: Provided id of the wrong type for class entity.news. Expected: class java.lang.Integer, got class java.lang.Byte at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:109) at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906) at org.hibernate.impl.SessionImpl.load(SessionImpl.java:823) at org.hibernate.impl.SessionImpl.load(SessionImpl.java:816) at test.main(test.java:30)

81,092

社区成员

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

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