Problem:
A user entity U1 has five fans U2, U3, U4, U5 and U6. Again, U1 is also a fan of users U3 and U5. How can this scenario be implemented using JPA?
Observation:
This is a case of a Many-to-Many relationship and the User entity is referring itself (self join).
Solution:
Edit the User entity class as shown below -
@ManyToMany
@JoinTable(name="USER_FAN",
@JoinColumn(name="user_id", referencedColumnName="id"),
@JoinColumn(name="fan_id",referencedColumnName="id"))
private ListmyFans = new ArrayList ();
@ManyToMany(mappedBy = "myFans")
private ListadmirerOf = new ArrayList ();
Hope it helps someone who wants a quick answer to this question.
Looks very promising,
ReplyDeletethanks for sharing this.
Ray.
what is solution if this relationship have properties?
ReplyDeleteMy scenario is bit complex.
ReplyDelete1. one application can provide services to many applications via different platform (ex. webmethod, web service, corba etc.. this is relationship property)
2. one application can consume services provided by many applications
Can you give an example of how to store such an entity ?
ReplyDeleteHi,
ReplyDeleteVery nice, this solution useful for me. Thanks for amazing post. Your all comment about JBPM Web Application are welcome.
Thanks.