开发者

Spring EntityManager Hibernate Exception Handling

In a Spring, JPA, Hibernate project I'm trying to get exception handling working. For the following code:

    @Repository("mscoutService")
    public class MScoutServiceImpl implements MScoutService, Serializable {

        @PersistenceContext
        private EntityManager em;

...
        @Override
        @Transactional
        public void deleteMission(Long missionId) {
           开发者_如何学JAVA try {
                Mission mis = em.find(Mission.class, missionId);
                em.remove(mis);
            } catch (Exception e) {
                handle_exception();
            }
        }

I'm trying to catch underlying hibernate/jdbc/db exceptions (for example when dependent entities are still present the remove will fail with a org.springframework.orm.hibernate3.HibernateJdbcException) and perform some actions. However the catch code is never reached (checked in the debugger).

I guess this has to do with the way Spring manages this, but I don't know just how I can catch exceptions during em.remove()...

Any help is appreciated!


This is because the exception occurs when the session gets flushed. And perhaps it gets flushed when the transaction is committed - i.e. by the spring proxy. If you want to manually flush, you can use entityManager.flush().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜