hibernate的save方法不起作用
publicintsendMessageToPartCgzs(finalStringfxrid,finalStringreceiveList,finalStringtit...
public int sendMessageToPartCgzs(final String fxrid,
final String receiveList, final String title,
final String content) {
final HibernateUtil hu = HibernateUtilFactory.getInstance()
.getHibernateUtil("default");
if (hu == null)
return 0;
return (Integer) new HibernateTemplate().run(hu,
new HibernateCallback() {
public Object execute() throws HibernateException {
try {
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyyMMdd");
// List<String> cgzList = null;
Sendmessage sm = new Sendmessage();
sm.setFxrid(fxrid);
sm.setTitle(title);
sm.setContent(content);
sm.setSenddate(sdf.format(new Date()));
hu.save(sm);//这个能行
String cgzids[] = receiveList.split(",");
for (int i = 0; i < cgzids.length; i++) {
Receivemessage rm = new Receivemessage();
rm.setSxrid(cgzids[i]);
rm.setTitle(title);
rm.setContent(content);
rm.setRead("未读");
rm.setReceivedate(sdf.format(new Date()));
System.out.println(cgzids[i]+"--"+title+"--"+content+"--"+sdf.format(new Date()));
hu.save(rm); //这个不行
System.out.println(rm.getId());
}
return 1;
} catch (Exception exp) {
exp.printStackTrace();
return 0;
}
}
});
} 展开
final String receiveList, final String title,
final String content) {
final HibernateUtil hu = HibernateUtilFactory.getInstance()
.getHibernateUtil("default");
if (hu == null)
return 0;
return (Integer) new HibernateTemplate().run(hu,
new HibernateCallback() {
public Object execute() throws HibernateException {
try {
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyyMMdd");
// List<String> cgzList = null;
Sendmessage sm = new Sendmessage();
sm.setFxrid(fxrid);
sm.setTitle(title);
sm.setContent(content);
sm.setSenddate(sdf.format(new Date()));
hu.save(sm);//这个能行
String cgzids[] = receiveList.split(",");
for (int i = 0; i < cgzids.length; i++) {
Receivemessage rm = new Receivemessage();
rm.setSxrid(cgzids[i]);
rm.setTitle(title);
rm.setContent(content);
rm.setRead("未读");
rm.setReceivedate(sdf.format(new Date()));
System.out.println(cgzids[i]+"--"+title+"--"+content+"--"+sdf.format(new Date()));
hu.save(rm); //这个不行
System.out.println(rm.getId());
}
return 1;
} catch (Exception exp) {
exp.printStackTrace();
return 0;
}
}
});
} 展开
2015-08-14 · 知道合伙人软件行家
关注
展开全部
Hibernate在执行增删改操作时必须将其放入事务环境内运行,如果save方法不报错但却不添加,则肯定没有开启事务!
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try{
session.save(...);
tx.commit();
}catch(Exception ex){
tx.rollback();
}finally{
session.close();
}
展开全部
sm.setFxrid 你在for循环里面补充上对应外面这个的代码 我怀疑是 唯一标示 没赋值 哦 看错了 有这部
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不行是怎么个不行法啊??报错还是啥啊??
追问
不报错的意思是不抛出任何异常,debug不出错误,返回1
追答
那可能是事务有问题,可能是for里面事务没有提交,你试试手动开启事务,手动提交
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询