这不起作用 - 我总是抛出IllegalArgumentException,并提出"尝试合并分离并再次尝试删除"的建议.
@PersistenceContext private EntityManager em;
@Resource private UserTransaction utx;
public void delete(EN entity) {
if (entity == null) return; // null-safe
EN target = entity;
try {
if (!em.contains(entity)) {
System.out.println("delete() entity not managed: " + entity);
utx.begin();
target = em.merge(entity);
utx.commit();
System.out.print("delete() this entity should now be managed: " + em.contains(target));
}
utx.begin();
em.remove(target);
utx.commit();
} catch (RollbackException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (HeuristicMixedException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (HeuristicRollbackException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, …Run Code Online (Sandbox Code Playgroud)