现在的位置: 首页 > 综合 > 正文

No TransactionManagerLookup specified

2017年12月26日 ⁄ 综合 ⁄ 共 1463字 ⁄ 字号 评论关闭

程序代码:

 

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>

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

 

抱歉!评论已关闭.