子类继承hibernatedaosupport后,由于hibernatedaosupport,setsessionfactory是使用final修飾的,無法重寫,沒有辦法使用註解的方式注入sessionfactroy
public final void setsessionfactory(sessionfactory sessionfactory) {
if (this.hibernatetemplate == null || sessionfactory != this.hibernatetemplate.getsessionfactory()) {
this.hibernatetemplate = createhibernatetemplate(sessionfactory);
}
}
我们可以变通一下,自己定义一个方法,这个方法去调用hibernatedaosupport 中的setsessionfacotry方法,达到注入sessionfactory的目的。
@autowired
public void setsupersessionfactory(sessionfactory sessionfactory) {
super.setsessionfactory(sessionfactory);
}
this.gethibernatetemplate().save()方法报错的问题
invaliddataaccessapiusageexception: write operations are not allowed in read-only mode (flushmode.manual): turn your session into flushmode.commit/auto or remove 'readonly' marker from transaction definition.
两步:
1.web.xml中打开懒加载
<!-- 懒加载 -->
<filter>
<filter-name>opensessioninviewfilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class>
</filter>
<filter-mapping>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.在执行save之前加这么一句:this.gethibernatetemplate().getsessionfactory().getcurrentsession().setflushmode(flushmode.auto)
this.gethibernatetemplate().getsessionfactory().getcurrentsession().setflushmode(flushmode.auto);
this.gethibernatetemplate().save(historyarcticlesrequest);