hibernate问题持续更新


问题一:
hibernate中一些不需要维持到数据库中的字段,使用@Transient 来维持


问题二:
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:1186)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:696)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
at com.bstek.scm.dao.daoImpl.VoucherDaoImpl.addVoucher(VoucherDaoImpl.java:31)
at com.bstek.scm.bo.fin.impl.VoucherManagerImpl.createVoucher(VoucherManagerImpl.java:48)
解决:
因为事务是在manager 层,然后方法createVoucher .create 不在配置的事务中.
事务配置
<aop:config> 
<!-- 切入点 -->
   <aop:advisor pointcut="execution(* com.bstek.scm.bo..*.*(..))"  advice-ref="txAdvice"/>
</aop:config>

<!-- 创建一个切面 -->
<tx:advice id="txAdvice" transaction-manager="TtransactionManager">
        <tx:attributes>
        <!-- 只要方法名 -->
        <tx:method name="add*" propagation="REQUIRED"
        isolation="DEFAULT" rollback-for="com.bstek.scm.common.exception.ManagerException,Exception"/>
        <tx:method name="delete*" propagation="REQUIRED"
        isolation="DEFAULT" rollback-for="com.bstek.scm.common.exception.ManagerException,Exception" />
        <tx:method name="update*" propagation="REQUIRED"
        isolation="DEFAULT" rollback-for="com.bstek.scm.common.exception.ManagerException,Exception"/>
        <tx:method name="save*" propagation="REQUIRED"
        isolation="DEFAULT" rollback-for="com.bstek.scm.common.exception.ManagerException,Exception"/>
        <tx:method name="insert*" propagation="REQUIRED"
        isolation="DEFAULT" rollback-for="com.bstek.scm.common.exception.ManagerException,Exception"/>
        <tx:method name="*" propagation="SUPPORTS" read-only="true"
        isolation="DEFAULT" />
        </tx:attributes>
</tx:advice>

所以 createVoucher 这个方法被认为是只读事务.
将 createVoucher 改为 addVoucher 就ok了.

你可能感兴趣的:(Hibernate)