Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!novia!news-out.readnews.com!news-xxxfer.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail From: "Matteo" Subject: Re: hibernate: mapping he Message-ID: X-Comment-To: comp.lang.java.databases Newsgroups: comp.lang.java.databases In-Reply-To: <_rmdnbPPGOc9l6vVnZ2dnUVZ_rWdnZ2d@comcast.com> References: <_rmdnbPPGOc9l6vVnZ2dnUVZ_rWdnZ2d@comcast.com> Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92] Lines: 97 Date: Wed, 27 Apr 2011 15:21:58 GMT NNTP-Posting-Host: 96.60.20.240 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1303917718 96.60.20.240 (Wed, 27 Apr 2011 10:21:58 CDT) NNTP-Posting-Date: Wed, 27 Apr 2011 10:21:58 CDT Organization: TDS.net Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.databases:139 To: comp.lang.java.databases Lew wrote: > Anyway, it is good that you found your answer. Since I don't know > Hibernate well at all, could you explain to the group what worked and > what exactly you had to do for it? I would learn a lot from that. Granted I'm an absolute beginner with Hibernate, so my solution is likely not to be perfect -and I'd like to hear a more expert advice on that, here's what I come up with: About the composite key of FRIENDS table: remember I had doubts (not about its correctness, as it is perfectly legal and common to have composite keys in DB tables). if you read through the Hibernate manual, they keep stressing composite keys as being the cause of all evil. From a certain point of view, they are right. A composite key can be translated to a surrogate key quite easily: (fieldA, fieldB) primary key NOT NULL is equal to surrogateKey PRIMARY KEY NOT NULL (fieldA, fieldB) UNIQUE NOT NULL This will save you from a lot of headhaches with Hibernate. Revised version of Friends table (I changed the name to Friendship) table FRIENDSHIP ( `id` int(11) unsigned NOT NULL PRIMARY KEY, `userID` int(11) unsigned NOT NULL, `friendID` int(11) unsigned NOT NULL, UNIQUE KEY (`userID`,`friendID`), CONSTRAINT `friendship_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`), CONSTRAINT `friendship_ibfk_2` FOREIGN KEY (`friendID`) REFERENCES `users` (`userID`) ) hibernate mapping: ----- Friendship.hbm.xml --------- ----- User.hbm.xml --------- As for MESSAGES table: Again, I think this is far from being perfect, but it works for me and for now I'm going to be happy with this solution, unless someone here would give me a better one (eg. I can think of reverting the Friendship table to FRIENDS, and having a java class Friend subclass User class...A Friend is a User itself in the end...) comments/suggestions? thanks Matteo --- * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet! --- Synchronet 3.15a-Win32 NewsLink 1.92 Time Warp of the Future BBS - telnet://time.synchro.net:24