-->
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.  [ 2 posts ] 
Author Message
 Post subject: hbm2ddl and property-ref do not create a foreign key
PostPosted: Thu Jun 03, 2004 6:13 pm 
Newbie

Joined: Thu Apr 29, 2004 6:57 pm
Posts: 4
I'm using hbm2ddl to generate my schema from a mapping file. If I leave off the "property-ref" attribute, it generates a foreign key constraint as expected. If I add the "property-ref" attribute, it does not. Is there a trick here? I could always manually add it after the generate but I'd like to understand why it isn't doing it. Thanks in advance.


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="eg">

<class name="User" table="users">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<property name="username" column="username" type="string" length="12"
not-null="true" unique="true" />
<property name="password" type="string" length="8" not-null="true" />
<property name="firstname" type="string" length="15" />
<property name="lastname" type="string" length="25" />
<property name="phone" type="string" length="20" />
<property name="email" type="string" length="40" />
</class>

<class name="Role" table="roles">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<property name="role" type="string" length="20"
not-null="true" unique="true"/>
<many-to-one name="username" property-ref="username"
class="eg.User" not-null="true" />
</class>

</hibernate-mapping>

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 05, 2004 10:03 am 
Beginner
Beginner

Joined: Thu May 13, 2004 5:51 am
Posts: 28
Not specifying a property-ref creates a foreign key, coz by default the primary key of the referenced table is used - User.id. If you provide a property reference, like "User.username" in your case, you are referencing another column (note this is not the relationship you want in the first place, right??) And User.username is not a key anyways, so you wont have a foreign constraint either. Best that hibernate can do is, ignore it rather than fail with an exception by not even creating a basic table schema.

If you want a relationship on username, you could consider adding that as the id?? or add an index over it with the index attribute or a unique constraint..cant say if that would work, never tried it.

--


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