Reputation: 1425
I've been trying variations on this, but I can't seem to get Ebean to recognize the constraint.
@ElementCollection
@CollectionTable(schema = "participants", joinColumns = @JoinColumn(name = "role_id"), uniqueConstraints = {
//The presence of actual column names in this line is an unfortunate consequence of eBean and JPA annotations
@UniqueConstraint(columnNames = {"integration_tag", "role_id"})
})
private final List<DefaultAccount> defaultIntegrationAccounts = new ArrayList<>();
For reference, here's DefaultAccount:
@Embeddable
public class DefaultAccount {
@NotNull
private final String integrationTag;
@NotNull
@ManyToOne
private Account account;
DefaultAccount(String integrationTag, Account account) {
this.integrationTag = integrationTag;
this.account = account;
}
public String getIntegrationTag() {
return integrationTag;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
Oddly enough, everything works well if DefaultAccount is made to be an entity rather than an embeddable, but this just goes against the domain modelling and I'd really rather not go down that route unless I absolutely have to...
Upvotes: 0
Views: 116
Reputation: 923
you can use a validation rule in the code or add an annotation to the integrationTag
field in the DefaultAccount
class to prevent duplicates in a list of DefaultAccounts
Using an annotation like @Column(unique=true)
will automatically make sure that no two DefaultAccounts
have the same integrationTag
and role_id
values.
You can also create a custom validation rule that checks for duplicates before saving the list, by creating an annotation, validator, and applying it to the list.
Just remember that Ebean may not automatically detect duplicate constraints in element collections, so it's best to test thoroughly before using it in production.
Upvotes: 0
Reputation:
In the limitations section of the Ebean documentation linked here, it says
@Embeddable class does not support @OneToMany / @ElementCollection.
As @Embeddable does not have identity the case of supporting collections gets ugly. At this stage there is no planned support for this.
And you have identified that changing the DefaultAccount
to an entity resolves the problem, so I think you have already identified the solution.
Upvotes: 0