-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How do I add a foreign key constraint?
PostPosted: Tue Oct 14, 2008 3:02 pm 
Beginner
Beginner

Joined: Wed Oct 08, 2008 10:59 am
Posts: 37
When I look at the DDL for a HSQLDB database then the following works:

alter table MESSAGES
add constraint FK_NEXT_MESSAGE
foreign key (NEXT_MESSAGE_ID)
references MESSAGES;

How would the same DDL command look like for a MySQL database ?
How can I add a foreign key constraint?

Peter


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 8:50 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Code:
    <class name="Message" table="MESSAGES">

       ...

      <many-to-one name="next" class="Message"
           unique="true"
           column="NEXT_MESSAGE_ID"
           foreign-key="FK_NEXT_MESSAGE "  />
    </class>


Notice that:

-the attribute "unique" makes this association one-to-one (and not many-to-one), since a message can have only one next message.

-the foreign key is not created by the foreing-key attribute, it just specifies the name of the FK.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 18, 2008 3:53 am 
Beginner
Beginner

Joined: Wed Oct 08, 2008 10:59 am
Posts: 37
gonzao_diaz wrote:
Code:
    <class name="Message" table="MESSAGES">

       ...

      <many-to-one name="next" class="Message"
           unique="true"
           column="NEXT_MESSAGE_ID"
           foreign-key="FK_NEXT_MESSAGE "  />
    </class>


Notice that:

-the attribute "unique" makes this association one-to-one (and not many-to-one), since a message can have only one next message.

-the foreign key is not created by the foreing-key attribute, it just specifies the name of the FK.


Is there a "normal" (=non XML) command as well for adding a constraint?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 18, 2008 12:13 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
The foreign-key="FK_NEXT_MESSAGE " and others that you see in the mapping file, only are relevant if you build the DB starting from your mapping file (using the Schema exporter or hibernate.hbm2ddl.auto)

If that is not your case, you use normal SQL DDL, like the one in your previous posting.

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.