I am trying to use the @CollectionOfElements annotation to save a set of value types along with my entity. Unfortunately, the entity is persisting but the value types are not. Can someone help me figure this out?
My entity class Account is shown below:
Code:
@Entity
public class Account implements Serializable {
...
private Set<AccountParty> accountParties = new HashSet<AccountParty>();
@CollectionOfElements(targetElement = AccountParty.class)
@JoinTable(name = "Account_AccountParties",
joinColumns = @JoinColumn(name = "account_id"))
public Set<AccountParty> getAccountParties() {
return accountParties;
}
public void setAccountParties(Set<AccountParty> accountParties) {
this.accountParties = accountParties;
}
public void addAccountParty(AccountParty accountParty) {
accountParties.add(accountParty);
}
}
The value type AccountParty is shown below:
Code:
@Embeddable
public class AccountParty implements Serializable {
private Party party;
private AccountRole role;
@ManyToOne(targetEntity=Party.class)
public Party getParty() {
return party;
}
public void setParty(Party party) {
this.party = party;
}
public AccountRole getRole() {
return role;
}
public void setRole(AccountRole role) {
this.role = role;
}
}
Finally, here's the code that persists an account with an AccountParty:
Code:
public void createAccount(Party owner) {
Account account = new Account();
account.addAccountParty(
new AccountParty(owner, AccountRole.owner));
entityManager.persist(account);
}
What am I doing wrong?
Thanks.
Naresh