-->
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.  [ 6 posts ] 
Author Message
 Post subject: Have Hibernate add FK constraint to many-to-one unique
PostPosted: Thu Jun 22, 2006 12:56 am 
Newbie

Joined: Tue Jun 13, 2006 9:03 am
Posts: 18
I'm currently using hibernate 3.1 and have a one-to-one relationship that uses a many-to-one mapping with a unique key. This is a unidirectional relationship with a mapping which looks like

Code:
<hibernate-mapping>

<class name="Package" table="PACKAGE">

<many-to-one name="label" column="labelId" class="LABELS" cascade="none" update="false" insert="false" not-null="true" unique="true"/>


The column 'labelId' gets created in the package table but there is no FK constraint on this labelId. I would like to add this constraint to make sure that the label exists in the label table in order for the package to be valid. How can I tell hibernate to do this in my mapping?

-nefi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 1:04 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Add 'foreign-key="nameOfYourForeignKey"'.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 1:27 am 
Newbie

Joined: Tue Jun 13, 2006 9:03 am
Posts: 18
Hi Tenwit,

I just tried what you suggested, dropped and recreated the tables, but to no avail. Still no foreign key constraint for the package table. BTW, I'm hitting SQLServer 2000, if this makes any difference.

-nefi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 1:43 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It's all described in the ref docs in section 20.1. According to that, it should work. Perhaps the foreign key is being generated on the table on the other side?

Examine the generated SQL. Use the --text or --output="filename" parameters to view the SQL, and that should help you deduce what's going wrong.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 10:31 am 
Newbie

Joined: Tue Jun 13, 2006 9:03 am
Posts: 18
I've looked at the docs and seems like it should work. However, the only thing I see on the PACKAGE table is that there is an index for labelId:

    name = UQ_PACKAGE_2F112023
    id = 2
    Uniq? = Y
    Column1 = labelId


Again, there is no dependent PACKAGE table for LABEL and there is no foreign key constraint for LABEL in the PACKAGE table.

As for the SQL output, I use hibernate.cfg.xml, define the properties:

<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>

and when I launch my app server, the tables get generated. I do not know out to use --output="filename" in the config file. Any suggestions?

-nefi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 5:50 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Don't use it in the config file, run hbm2ddl manually. It's all described in the ref docs, section 20.1.2.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.