首页  »   软件架构设计

SSH框架遇见有关问题汇总

网友分享于:2014-04-23  浏览:4次
SSH框架遇见问题汇总

Hibernate:

  1、spring与hibernate整合数据提交时,出现session不一样情况就会出现:

代码:

   

private SessionFactory factory = null;
	public SessionFactory getFactory() {
		return factory;
	}
	public void setFactory(SessionFactory factory) {
		this.factory = factory;
	}
	public User save(User user){
		Session session = factory.getCurrentSession();
		session.save(user);
		session.getTransaction().commit();
		return null;
	}

 出现错误:

Stacktraces

org.hibernate.HibernateException: save is not valid without active transaction
    org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:341)
    $Proxy12.save(Unknown Source)
    com.ssh.service.IUserService.save(IUserService.java:28)
    com.ssh.action.UserAction.ecut(UserAction.java:23)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   问题原因:原因是session不一样导致,开始在spring 配置文件applicationContext.xml文件中配了“hibernate.current_session_context_class=thread”未解决;

     解决办法: 代码修改:

private SessionFactory factory = null;
	public SessionFactory getFactory() {
		return factory;
	}
	public void setFactory(SessionFactory factory) {
		this.factory = factory;
	}
	public User save(User user){
		Session session = factory.getCurrentSession();
		 Transaction tran=session.beginTransaction();
		session.save(user);
		tran.commit();
		return null;
	}

 

  

相关解决方案

最新解决方案