Spring2.0事务配置问题(重写父类方法后出错)

由于一些功能模块用到的Service和DAO层的方法类似,现将类似的方法进行重构,提出基本的Service和DAO,真正用到的Service和DAO只要实现或者继承即可。在重构后遇到事务配置问题,具体如下。

 

抛出异常:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
 at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1085)
 at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:624)
 at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:362)
 at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:622)

 

 

Spring2.0配置:

	<aop:config>
		<aop:advisor
			pointcut="execution(* aumy2008.service.impl..*.*(..)) "
			advice-ref="txAdviceRet" />
	</aop:config>

	<tx:advice id="txAdviceRet"
		transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"
				propagation="REQUIRED" />
			<tx:method name="load*" read-only="true"
				propagation="REQUIRED" />
			<tx:method name="find*" read-only="true"
				propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

 

重构后的基本接口和实现类的部分代码:

 

public interface IBaseService<T> {

	public Long add(T t);

	public void modify(T t);

	public void addOrModify(T t);

	......
}

 

public class BaseService<T> implements IBaseService<T> {

	private IRBaseDao<T> irBaseDao;

	private boolean isSwitchCharsetTranslate;

	public BaseService(IRBaseDao<T> irBaseDao, boolean isSwitchCharsetTranslate)
			throws Exception {
		if (irBaseDao == null) {
			throw new Exception("irBaseDao may not be null");
		}
		this.irBaseDao = irBaseDao;
		this.isSwitchCharsetTranslate = isSwitchCharsetTranslate;
	}


	public Long add(T t) {
		return (Long) this.irBaseDao.save(t);
	}


	public void addOrModify(T t) {
		this.irBaseDao.saveOrUpdate(t);
	}

	public void modify(T t) {
		this.irBaseDao.update(t);
	}

	......
}

 

public interface IRBaseDao<T> {

	public Serializable save(Object o);	

	public void update(Object o);
	
	public void saveOrUpdate(Object o);

	......
}
 
public class RBaseDao<T>  extends HibernateDaoSupport implements IRBaseDao<T> {

	public Serializable save(Object o) {
		Serializable id = getHibernateTemplate().save(o);
		this.flush();
		return id;
	}

	public void update(Object o) {		
		getHibernateTemplate().update(o);
		this.flush();
	}
	
	public void saveOrUpdate(Object o) {
		getHibernateTemplate().saveOrUpdate(o);
		this.flush();
	}

	......
}

 具体业务的接口和实现类的部分代码:

public interface ITypeService extends IBaseService<Type> {

}

 

public class TypeService extends BaseService<Type> implements ITypeService {
	private IRBaseDao<Type> typeDAO;

	public boolean isSwitchCharsetTranslate;
	public TypeService(IRBaseDao<Type> typeDAO, boolean isSwitchCharsetTranslate)
			throws Exception {
		super(typeDAO, isSwitchCharsetTranslate);
		this.typeDAO = typeDAO;
		this.isSwitchCharsetTranslate = isSwitchCharsetTranslate;
	}

	public void addOrModify(Type t) {
		typeDAO.saveOrUpdate(toDBValue(t));
	}

	public Long add(Type t) {
		return (Long) typeDAO.save(toDBValue(t));
	}

	public void modify(Type t) {
		typeDAO.update(toDBValue(t));
	}

	private Type toDBValue(Type type) {
		......
		return type;
	}

}
 
public interface ITypeDAO extends IBaseDao<Type> {

}
 
public class TypeDAO extends RBaseDao<Type> implements ITypeDAO {

}


如果TypeService不重写父类的方法,运行正常
代码如:

public class TypeService extends BaseService<Type> implements ITypeService {

	public TypeService(IRBaseDao<Type> typeDAO, boolean isSwitchCharsetTranslate)
			throws Exception {
		super(typeDAO, isSwitchCharsetTranslate);
	}

}

 

我现在就是想在TypeService类中实现字符集的转换,需要重写add等三个方法,当重写后,运行就报开始给出的异常。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值