-
1. Re: CMR Many to many relationships with 3 beans
raist_majere Nov 28, 2004 11:16 PM (in response to jocky4)If you want to use CMP for your app, then you must normalize your design. That means that if you have to use a relationship with attributes (as in your case) or relationships between more than two entities at the same time, you must define a third entity (or class) between the two related entities and put the relation attributes there, then make a relation to the related entities...
I think I'll be clearer with an example. In your case, you should make a third entity, named Sale (for example) which will have a M:N relation to Book, and another M:N relation to User, as well as the attributes of the relation, in your case buyDate. Of course, if you use this approach, you must delete the relation you have now between Books and Users. The only problem you may encounter using this approach is that the primary key field cannot be composed of CMR fields, so the best choice you can make is to have an autogenerated PK field.
Hope this helps. -
2. Re: CMR Many to many relationships with 3 beans
jocky4 Nov 29, 2004 7:07 AM (in response to jocky4)Thank you for response.
I'm happy because I already implements my bean correctly.
In my case, user can buy a book only one time. So the relation between User and Sale is a N:1 relantionships.
I've done a mistake in the PK of the Sale bean. I created a composed PK :public class BookUserPK { public Book book; public User user; public BookUserPK() {} public BookUserPK(Book pBook, User pUser) { this.book = pBook; this.user = pUser; } public boolean equals(Object pObj) { if (! (pObj instanceof BookUserPK)) return false; BookUserPK key = (BookUserPK) pObj; return book.equals(key.book) && user.equals(key.user); } public int hashCode() { return 65521 * book.hashCode() + user.hashCode(); } }
It seem to be uncorrect.
How can I do ?
My tabel bookuser as three columns :
book_id
user_id
buy_date
SQL PK is book_id + user_id
Should I add a fourth column ?
How can I do an autogenerated PK field ?
Thank you
Jo -
3. Re: CMR Many to many relationships with 3 beans
raist_majere Nov 30, 2004 11:54 PM (in response to jocky4)After reading your last post I realized I made a mistake :P: the Sale bean has to have 1:N relationships instead of M:N with User and Book beans.
About generating PK fields, you can take a look at this page:
http://docs.jboss.org/jbossas/admindevel326/html/ch11.chapter.html#ch11.pkgen.sect