It's not that, the database model is enforced with foreign key constraints; also, the delete action is cascaded and all it's "children" are correctly deleted too.
I believe the problem is due to the cascading refresh that follows the em.refresh(company). If I don't cascade the refresh action in the Company.departments relationship, then I don't get the error anymore. But like this, the Department who's Employee got deleted isn't refreshed and the Employee still shows up, even if it's not in the database anymore.
Here's the full stack of what happens on the em.refresh(company) call.
Code:
10:37:22,366 ERROR [STDERR] javax.ejb.EJBTransactionRolledbackException: javax.persistence.EntityNotFoundException: org.hibernate.UnresolvableObjectException: No row with the given identifier exists: [com.example.Employee#707]
10:37:22,386 ERROR [STDERR] Caused by: javax.persistence.EntityNotFoundException: org.hibernate.UnresolvableObjectException: No row with the given identifier exists: [com.example.Employee#707]
10:37:22,386 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:169)
10:37:22,386 ERROR [STDERR] at org.jboss.ejb3.entity.InjectedEntityManager.refresh(InjectedEntityManager.java:156)
10:37:22,386 ERROR [STDERR] at com.friday.dao.ejb.GenericDAOBean.refresh(GenericDAOBean.java:44)
10:37:22,386 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
10:37:22,386 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
10:37:22,386 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
10:37:22,386 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
10:37:22,386 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:109)
10:37:22,387 ERROR [STDERR] at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
10:37:22,387 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
10:37:22,387 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
10:37:22,387 ERROR [STDERR] ... 165 more
10:37:22,389 ERROR [STDERR] Caused by: org.hibernate.UnresolvableObjectException: No row with the given identifier exists: [com.example.Employee#707]
10:37:22,389 ERROR [STDERR] at org.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectException.java:42)
10:37:22,389 ERROR [STDERR] at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:126)
10:37:22,389 ERROR [STDERR] at org.hibernate.impl.SessionImpl.fireRefresh(SessionImpl.java:924)
10:37:22,389 ERROR [STDERR] at org.hibernate.impl.SessionImpl.refresh(SessionImpl.java:907)
10:37:22,389 ERROR [STDERR] at org.hibernate.engine.CascadingAction$4.cascade(CascadingAction.java:92)
10:37:22,389 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
10:37:22,389 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
10:37:22,389 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
10:37:22,389 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290)
10:37:22,389 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185)
10:37:22,390 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160)
10:37:22,390 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
10:37:22,390 ERROR [STDERR] at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
10:37:22,390 ERROR [STDERR] at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:99)
10:37:22,390 ERROR [STDERR] at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:39)
10:37:22,390 ERROR [STDERR] at org.hibernate.impl.SessionImpl.fireRefresh(SessionImpl.java:915)
10:37:22,390 ERROR [STDERR] at org.hibernate.impl.SessionImpl.refresh(SessionImpl.java:899)
10:37:22,390 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:166)
10:37:22,390 ERROR [STDERR] ... 175 more