I have a JPA-persisted object model that contains a many-to-one relationship: an Account
has many Transactions
. A Transaction
has one
You need to set Transaction for every Account.
foreach(Account account : accounts){
account.setTransaction(transactionObj);
}
Or it colud be enough (if appropriate) to set ids to null on many side.
// list of existing accounts
List<Account> accounts = new ArrayList<>(transactionObj.getAccounts());
foreach(Account account : accounts){
account.setId(null);
}
transactionObj.setAccounts(accounts);
// just persist transactionObj using EntityManager merge() method.
Since this is a very common question, I wrote this article, on which this answer is based on.
In order to fix the problem you need to follow these steps:
So, you need to remove the @CascadeType.ALL
from the @ManyToOne
association. Child entities should not cascade to parent associations. Only parent entities should cascade to child entities.
@ManyToOne(fetch= FetchType.LAZY)
Notice that I set the fetch
attribute to FetchType.LAZY
because eager fetching is very bad for performance.
Whenever you have a bidirectional association, you need to synchronize both sides using addChild
and removeChild
methods in the parent entity:
public void addTransaction(Transaction transaction) {
transcations.add(transaction);
transaction.setAccount(this);
}
public void removeTransaction(Transaction transaction) {
transcations.remove(transaction);
transaction.setAccount(null);
}
For more details about why it's important to synchronize both ends of a bidirectional association, check out this article.
In your entity definition, you're not specifying the @JoinColumn for the Account
joined to a Transaction
. You'll want something like this:
@Entity
public class Transaction {
@ManyToOne(cascade = {CascadeType.ALL},fetch= FetchType.EAGER)
@JoinColumn(name = "accountId", referencedColumnName = "id")
private Account fromAccount;
}
EDIT: Well, I guess that would be useful if you were using the @Table
annotation on your class. Heh. :)
In my case I was committing transaction when persist method was used. On changing persist to save method , it got resolved.
Don't pass id(pk) to persist method or try save() method instead of persist().
cascadeType.MERGE,fetch= FetchType.LAZY