just a short understanding-question:
User user =new User(); user = em.merge(user); em.persist(user); System.out.println(user.getId());
16:54:15,500 INFO [STDOUT] javax.ejb.EJBException: null; CausedByException is: detached entity passed to persist: com.example.entity.User 16:54:15,501 INFO [STDOUT] at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46) 16:54:15,501 INFO [STDOUT] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:70) 16:54:15,502 INFO [STDOUT] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134) ..... ....
The new user is considered detached because you probably have in your Db a user having the same id as the id set when the user is created.
So merge actually update the user, and then since user is managed persist() is a no-op