I have 2 objects: Account and AccountRole .
public class Account { private AccountRole accountRole; @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) public AccountRole getAccountRole() { return accountRole; }
.
public class AccountRole { private Collection<Account> accounts = new ArrayList<Account>(); @OneToMany(mappedBy = "accountRole", fetch = FetchType.EAGER) public Collection<Account> getAccounts() { return accounts; }
The problem occurs when I take an account from the database and try to save my Account . At this point, I just created my account and the role already exists in db.
AccountRole role = accountService.getRoleFromDatabase(AccountRoles.ROLE_USER); account.setAccountRole(role); //setting both ways, as suggested public void setAccountRole(AccountRole accountRole) { accountRole.addAccount(this); this.accountRole = accountRole; } entityManager.persist(account); // finally in my DAO
I read this: JPA / Hibernate: the remote object was handed over for storage. And as I understand it, I have to set the values โโof entities from both directions, so what I do in my setter.
The error still appears.
org.hibernate.PersistentObjectException: detached entity passed to persist: foo.bar.pojo.AccountRole
java hibernate jpa entitymanager
Jaanus
source share