我一直在我的服务层中使用 @Transactional
注释。但要解决由于延迟加载 View 而导致的错误,我必须使用 Open Session in View Filter
。此后,在不使用@Transaction 本身的情况下,将打开 session 并开始事务。那么这是否意味着不需要@Transactions?那么在服务层中将如何处理事务和回滚?
最佳答案
The javadoc解释一下:
This filter makes Hibernate Sessions available via the current thread, which will be autodetected by transaction managers. It is suitable for service layer transactions via HibernateTransactionManager or JtaTransactionManager as well as for non-transactional execution (if configured appropriately).
NOTE: This filter will by default not flush the Hibernate Session, with the flush mode set to FlushMode.NEVER. It assumes to be used in combination with service layer transactions that care for the flushing: The active transaction manager will temporarily change the flush mode to FlushMode.AUTO during a read-write transaction, with the flush mode reset to FlushMode.NEVER at the end of each transaction. If you intend to use this filter without transactions, consider changing the default flush mode (through the "flushMode" property).
关于java - 在 View 中打开 session 与@Transactional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16465269/