No TransactionManagerLookup specified

程序代码:

 

public void addStudent(Student student) {
  Session session=null;
  try {
   //通过工具类HibernateSessionFactory的方法getSessionFactory()获取Session
//   session=HibernateSessionFactory.getSessionFactory().getCurrentSession();
   session=HibernateSessionFactory.getSession();
   session.beginTransaction();
   session.save(student);
   Inform inform =new Inform();
   inform.setType("新报名");
   inform.setDetail("身体健康");
   inform.setTime(new Date());
   //把对象inform通过管理对象logManageraocung起来
   InformManager logManager=new InformManagerAdd();
   logManager.addInform(inform);
   session.getTransaction().commit();
   
  } catch (Exception e) {
   System.out.println("添加失败");
   e.printStackTrace();
   
   session.getTransaction().rollback();
  }
  
 }


 public void addInform(Inform inform) {

  //从当前线程中获取session,并把对象inform保存到对象session中
  HibernateSessionFactory.getSessionFactory().getCurrentSession().save(inform);
//  HibernateSessionFactory.getSession().save(inform);
 }

public static void main(String[] args) {
  Student student=new Student();
  
  student.setName("cjg");
  
  StudentManager studentManager=new StudentManagerAdd();
  
  studentManager.addStudent(student);
 }

出错原因:我忘记把两个方法中用的sesiion一致,因为混用了所以报错,

要么同时写session=HibernateSessionFactory.getSession()来获取session

然后hibernate中配置为

<property name="current_session_context_class">jta</property>

要不就是用session=HibernateSessionFactory.getSessionFactory().getCurrentSession();来获取session

然后hibernate中的配置为:<property name="current_session_context_class">thread</property>

这样保持一致后就应该不会出错了!

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值