Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.databases > #139

Re: hibernate: mapping he

From "Matteo" <matteo@THRWHITE.remove-dii-this>
Subject Re: hibernate: mapping he
Message-ID <g18o8b$s52$1@nnrp.ngi.it> (permalink)
Newsgroups comp.lang.java.databases
References <_rmdnbPPGOc9l6vVnZ2dnUVZ_rWdnZ2d@comcast.com>
Date 2011-04-27 15:21 +0000
Organization TDS.net

Show all headers | View raw


  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 ---------
<class name="Friendship" table="friendship">
     <id column="id" name="id">
       <generator class="native"/>
     </id>
     <properties ...
     <many-to-one class="User" column="userID" name="friendA" 
not-null="true"/>
     <many-to-one class="User" column="friendID" name="friendB" 
not-null="true"/>
</class>

----- User.hbm.xml ---------
<class name="User" table="Users">
     <id column="userID" name="id">
       <generator class="native"/>
     </id>
     <properties ...

     <set name="friends" table="friends" inverse="true" cascade="all">
       <key column="friendID"/>
       <one-to-many class="Friendship"/>
     </set>

     <set name="messages" table="messages" cascade="all" inverse="true">
       <key column="receiver"/>
       <one-to-many class="Message"/>
     </set>
</class>


As for MESSAGES table:
<class name="Message" table="messages">
     <id column="messageID" name="id">
       <generator class="native"/>
     </id>
     <many-to-one class="User" column="sender" name="sender" 
not-null="true"/>
     <many-to-one class="User" column="receiver" name="receiver" 
not-null="true"/>
</class>


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

Back to comp.lang.java.databases | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

hibernate: mapping help "Matteo" <matteo@THRWHITE.remove-dii-this> - 2011-04-27 15:21 +0000
  Re: hibernate: mapping he "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:21 +0000
    Re: hibernate: mapping he "Matteo" <matteo@THRWHITE.remove-dii-this> - 2011-04-27 15:21 +0000
      Re: hibernate: mapping he "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:21 +0000
        Re: hibernate: mapping he "Matteo" <matteo@THRWHITE.remove-dii-this> - 2011-04-27 15:21 +0000
          Re: hibernate: mapping he "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:21 +0000

csiph-web