By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,628 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,628 IT Pros & Developers. It's quick & easy.

Inverse uniqueness in EJB 3.0 many-to-many relation

P: 168

I have an @Entity class, Person, with a @ManyToMany relation with itself. This relation is the friend association. The code of the class is as follows:

@Table(name = "PERSON", uniqueConstraints={@UniqueConstraint(columnNames={ "PERSON_NAME"})})
public class Person {

@Column(name = "PERSON_ID")
private Long id;

@Column(name = "PERSON_NAME")
private String name;

@JoinTable(name = "FRIENDS",
joinColumns = @JoinColumn(name = "PERSON_A"),
inverseJoinColumns = @JoinColumn(name = "PERSON_B"))
private Set<Person> friends = new HashSet<Person>();

/*contructors, getters, setters, etc*/

The way the class is defined right now allows for the simultaneous existence of <peter, john> and <john, peter> relations when there should only be one (because john and peter are friends implies peter an john are friends). Does the Java Persistence API supply a mechanism to enforce this type of behaviour?

Any help would be apreciated.

Thanks in advance,
Feb 7 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.