I think you would be better off with two many-to-one relationships. If you do a single many-to-many you will not have a nice place to set the alias. Plus you may want or need more attributes to the identity, such as authorization rules.
So, with EntityManager+Annotations you would end up with:
Code:
@Entity
class Domain {
name;
@OneToMany(fetch = FetchType.LAZY, mappedBy="domain")
Set<DomainIdentity> domainIdentities;
}
@Entity
class DomainIdentity {
@ManyToOne Domain domain;
@ManyToOne Identity identity;
String alias;
}
@Entity
class Identity {
@OneToMany(fetch = FetchType.LAZY, mappedBy="identity";
Set<DomainIdentity> domainIdentities;
String name;
// ... other properties
}
I removed the private/public modifiers and getters/setters to make it more readable.
If you want a shortcut from the identity to the domains, you could make a read-only many-to-many property using the table that maps DomainIdentity as the join-table. However, I personally do not recommend this.
Hope it helps. Regards,
Henrique