-->
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: Foreign key constrain
PostPosted: Mon Jan 26, 2004 6:00 am 
Beginner
Beginner

Joined: Fri Jan 02, 2004 3:27 am
Posts: 23
Location: Hong Kong
During one-to-many relation. (e.g Order -> Item ). Is there any way to declare such relation in hibernate xml file such that Order is not allowed to be deleted unless there is no items associated ?

Or the foreign key constrain is not a good suitable approach?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 6:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If I am not misstaken, the foreign key constraint should allready be generated by hbm2ddl. Or do you mean something else?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 10:57 pm 
Beginner
Beginner

Joined: Fri Jan 02, 2004 3:27 am
Posts: 23
Location: Hong Kong
I am using MySql 4.0.15-max-nt in Windows XP.

When I create the mapping many-to-one association from Order -> Item
I want it is not allowed to execute ( eg. throw an exception )
Code:
session.delete(order)  // remove the order by hibernate API

when Order have one or more Item.

Currently, I can delete the Order even there is Item assoicaite with it.
Code:
// xml in Item

<many-to-one name="purchaseOrder"
  class="hibernate.PurchaseOrder"
  cascade="none"
  outer-join="auto"
  update="true"
  insert="true"
  column="purchaseOrder"/>

Code:
// xml in Order

<bag name="purchaseItems"
  lazy="false"
  inverse="false"
  cascade="none">
    <key column="purchaseOrder"/>
    <one-to-many class="hibernate.PurchaseItem"/>
</bag>


The xml is genereted by Xdoclet


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 11:04 pm 
Beginner
Beginner

Joined: Fri Jan 02, 2004 3:27 am
Posts: 23
Location: Hong Kong
The following is the SQL generated from Hibernate.

Code:
create table Item (
   internalId BIGINT NOT NULL AUTO_INCREMENT,
   pOrder BIGINT,
   primary key (internalId)
)
create table POrder (
   pId BIGINT NOT NULL AUTO_INCREMENT,
   primary key (pId)
)

alter table Item add index (pOrder), add constraint FK9BDADFD4B0358ACD foreign key (pOrder) references POrder (pId)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 11:15 pm 
Beginner
Beginner

Joined: Fri Jan 02, 2004 3:27 am
Posts: 23
Location: Hong Kong
Sorry, the above SQL 's variable does not match the xml provided. The item should be purchaseItem and POrder should be purchaseOrder. They are extracted from part of the code and I have rename some of the varible to make it better understanding.

Anyway, am I got something wrong in the hbm2ddl ?


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.