I have 2 entities : Account and AccountRole.
public class Account {
private AccountRole accountRole;
@ManyToOne(cascade = Ca
It looks like you leave the transaction during your processing, so the accountRole gets detached, or it is already detached for other reasons.
A call to entityManager.merge(accountRole) before calling entityManager.persist(account) should fix it.
EDIT: Unfortunately, if you cannot be sure if the accountRole already exists in the DB, you will have to check it by querying. If it exists - merge, if not - persist. It is indeed a hassle, but I have not yet seen a better workaround.
EDIT2: The entity you pass to the merge method will remain detached - the managed entity will be returned by the merge, so you would need to merge first, then set the reference on the account to the return value of the merge.