-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to map an optional foreign key
PostPosted: Tue Jan 29, 2008 9:05 am 
Newbie

Joined: Wed Jan 23, 2008 11:03 am
Posts: 2
Hi everyone,

I have two tables parent and child. Child of course contains a foreign key so it can references his parent. But according to my business model, a Child can be orphan (it has no parent) so its foreign key should be null or zero in that case.

The problem arises from the fact that the generated DDL by hibernate (using Oracle dialect) creates a foreign key constraint (Child references Parent). So every time I try to add a new orphan child (without linking it to a parent), it throws an SQLException (violation of constraint).

I have added not-null="false" to both end of the association (so foreign key could and can be null) but nothing changed. I have used many-to-one from the child's side and one-to-many from the parent's side with inverse=true.

Please can anyone tell me how to make this association optional so I can add orphan children (I do not want to use dirty techniques and just drop the constraint from Oracle, I want hibernate to be aware of this too).

if you need more details I will be happy to add it.

thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 30, 2008 12:08 am 
Newbie

Joined: Wed Sep 19, 2007 11:30 pm
Posts: 12
Location: indonesia
can you paste the hbm.cfg.xml here?

_________________
An incredible programmer is worthier than a common programmer because of his/her ideas, not his/her codes.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 11, 2008 9:47 am 
Newbie

Joined: Mon Feb 11, 2008 9:41 am
Posts: 2
Hi!

I have the same problem. How do I model a optional foreign key????

Here are my mapping files:

Code:
<hibernate-mapping>

    <class
        name="metainf.anwendungskern.metainfoManagement.db.AttributeMetainformation"
        table="ET_ATMI_ATTRIBUTE_META_INFORMATION">

        <composite-id name="primaryKey" class="metainf.anwendungskern.metainfoManagement.db.AttributeMetainformationPK">
           <key-many-to-one name="attribute" class="metainf.anwendungskern.attributeManagement.db.Attribute" column="ATTR_KEY"/>
           <key-many-to-one name="useCase" class="metainf.anwendungskern.usecaseManagement.db.UseCase" column="USE_CASE_CONFIG"/>
      </composite-id>

   <many-to-one
            name="attributeGroup"
            cascade="none"
            column="ATGR_KEY"
            foreign-key="FK_ATTRIBUTEGROUP"
            lazy="false"
            fetch="join"/>
       
<!-- This one should be optional -->
        <many-to-one
            name="attributeClass"
            cascade="none"
            lazy="false"
            column="ATCL_KEY"
            foreign-key="FK_ATTRIBUTECLASS"
            fetch="join"/>

        ... something other...   

    </class>


</hibernate-mapping>


Code:
<hibernate-mapping>

    <class
        name="metainf.anwendungskern.metainfoManagement.db.AttributeClass"
        table="ET_ATCL_ATTRIBUTE_CLASS"
        batch-size="5">

        <id name="attributeClassKey" type="metainf.db.TrimmedString">
            <column name="ATCL_KEY"/>
        </id>
       
    </class>

</hibernate-mapping>



AttributeClass should be optional. Have anyone a solution?

Thanks in advance.

regards,
Pierre


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 1:39 pm 
Newbie

Joined: Thu Feb 21, 2008 1:37 pm
Posts: 3
I had a similar need and after doing a lot of googling found this undocumented feature in hibernate. You can use the foreign key name as "none" to remove the foreign key constraint. Hope this helps and I hope they make this a documented feature in the future releases.

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 1:59 pm 
Newbie

Joined: Thu Feb 21, 2008 1:37 pm
Posts: 3
I found this info on this very informative blog. Hope it will help others too:

http://blog.xebia.com/2007/02/05/let-hi ... our-world/

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


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