-->
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.  [ 9 posts ] 
Author Message
 Post subject: Forcing hbm2ddl to not generate an FK
PostPosted: Wed Aug 08, 2007 3:56 am 
Newbie

Joined: Fri Apr 06, 2007 1:54 am
Posts: 14
Hibernate version:
3.2

Mapping documents:
Code:
<class name="Foo" table="foo">
  <id name="id" length="16" access="field" />

  ...

  <set name="usedBy" inverse="true">
    <key column="foo_id" />
    <one-to-many class="Bar" not-found="ignore" />
  </set>

</class>

Name and version of the database you are using:
MySQL 5.x

Hello

I have classes Foo and Bar.
Foo has a Set<Bar> usedBy;
So in my table bar there is a field "foo_id" which points to field "id" of table foo.

hbm2ddl tries to generate a foreign key for that relation, but I don't want to. Is it possible to force hbm2ddl to not generate an FK?

In the hbm2ddl documentation (Customizing the schema) it says: "Note that inverse="true" sides will not be considered by SchemaExport."
But, both SchemaUpdate and SchemaExport do generate this FK.

I don't want an FK because entries from table "foo" might be deleted later on. And that's why I also use not-found="ignore". Right now this is not possible with this FK.

Any solution for this problem?
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 4:21 am 
Newbie

Joined: Fri Apr 06, 2007 1:54 am
Posts: 14
OK just found it.
I need to specify foreign-key="none" to cancel FK generation for that field.
This is not mentioned in the hibernate documentation, nor in the Java Persistence with Hibernate book. I found it at a cached version of hibernate 2.1.9 changelog after hardcore googling:

Quote:
Changes in version 2.1.9 (xx.x.xxxx)
------------------------------------
* TimesTenDialect now supported (look at TimesTenDialect for certain limitations)
* foreign-key="none" can be used to disable generation of a foreign key.


Maybe it should be included in the documentation in section 20. Toolset Guide

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 6:08 am 
Newbie

Joined: Tue Aug 28, 2007 3:18 am
Posts: 13
Hi,
I am new to the Hibernate as well as in the forum.
Can you please specify where should I set the foreign-key="none" to cancel the generation of foreign-key reference ?

thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 7:38 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
Judging from the mapping DTD, set it as an attribute of they key element.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 4:42 am 
Newbie

Joined: Thu Aug 30, 2007 4:39 am
Posts: 2
Location: Roma, Italy
morales1996 wrote:
OK just found it.
I need to specify foreign-key="none" to cancel FK generation for that field.
This is not mentioned in the hibernate documentation, nor in the Java Persistence with Hibernate book. I found it at a cached version of hibernate 2.1.9 changelog after hardcore googling:

Quote:
Changes in version 2.1.9 (xx.x.xxxx)
------------------------------------
* TimesTenDialect now supported (look at TimesTenDialect for certain limitations)
* foreign-key="none" can be used to disable generation of a foreign key.


Maybe it should be included in the documentation in section 20. Toolset Guide

Thanks


This post of yours was very very useful to me, you saved my day ;-)! Thanks a lot for putting down your solution; it's a shame this isn't (yet) in the reference manual!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 12, 2007 9:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
put it in jira for hibernate core/docs.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 2:45 pm 
Regular
Regular

Joined: Wed Nov 17, 2004 11:49 am
Posts: 65
Location: Pittsburgh
Do you know if this is supported via annotations? I haven't found it yet, if it is...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 2:32 pm 
Newbie

Joined: Thu Feb 21, 2008 1:37 pm
Posts: 3
You can do it via annotations too like so:
@ManyToOne
@ForeignKey(name = "none")
@JoinColumn(name = "user_id", nullable = false)

_________________
Are you a cricket fan?
Visit us at: http://cricdigs.com


Top
 Profile  
 
 Post subject: Re: Forcing hbm2ddl to not generate an FK
PostPosted: Wed Jan 13, 2010 11:38 am 
Regular
Regular

Joined: Thu May 07, 2009 5:56 am
Posts: 94
Location: Toulouse, France
Anyone knows how I can get the same result (i.e. turn off the fk) but I'm using only JPA 1.0 annotations (rather metadata tags in orml.xml) ?

Thanks,

_________________
everything should be made as simple as possible, but not simpler (AE)


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